Source code: com/arranger/jarl/shell/views/MainJarlShellFrame.java
1 package com.arranger.jarl.shell.views;
2
3 import com.arranger.jarl.shell.JarlShell;
4 import com.arranger.jarl.shell.views.mainShellPanes.*;
5 import com.arranger.jarl.shell.models.JarlContextModel;
6 import com.arranger.jarl.shell.models.BaseModel;
7 import com.arranger.jarl.shell.models.ViewPrefModel;
8 import com.arranger.jarl.util.ImageUtil;
9 import com.arranger.jarl.util.IconUtil;
10
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.beans.PropertyVetoException;
15
16 /**
17 * MainJarlShellFrame created on Apr 19, 2003
18 */
19 public class MainJarlShellFrame extends JFrame {
20
21 protected static final int INTERNAL_OFFSET_X = 30, INTERNAL_OFFSET_Y = 30;
22
23 protected JarlShell m_jarlShell;
24 protected JDesktopPane m_desktop;
25 protected IMainShellPane m_mspJarlFile;
26 protected IMainShellPane m_mspFrameSlider;
27 protected IMainShellPane m_mspOptionsPane;
28 protected IMainShellPane m_mspRenderPane;
29 protected IMainShellPane m_mspRenderStatusPane;
30 protected IMainShellPane m_mspPreviewAnimationPane;
31 protected Component m_topPane;
32
33 public MainJarlShellFrame(JarlShell jarlShell, String title) throws HeadlessException {
34 super(title);
35 m_jarlShell = jarlShell;
36
37 //initialize the panes
38 initializePanes();
39
40 //scroll & status pane
41 JScrollPane scroller = createScrollPane();
42 JPanel statusPane = createStatusPanel();
43
44 //splitter
45 JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, statusPane, scroller);
46 setContentPane(splitPane);
47
48 //menu
49 setJMenuBar(createMenuBar());
50
51 //frame
52 Dimension dim = getToolkit().getScreenSize();
53 setSize(800, 600);
54 setLocation(dim.width / 2 - getWidth() / 2, dim.height / 2 - getHeight() / 2);
55 m_desktop.setPreferredSize(new Dimension(1600, 1200));
56
57 //Quit this app when the big window closes.
58 addWindowListener(new WindowAdapter() {
59 public void windowClosing(WindowEvent e) {
60 shutDown();
61 }
62 });
63
64 //icon
65 try {
66 Image image = ImageUtil.loadResourceImage(/*"jarl.jpg"*/IconUtil.DEFAULT_TREE_LEAF_ICON);
67 /*image = ImageUtil.createTransparentImage(image, Color.black);*/
68 setIconImage(image);
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72
73 show();
74 }
75
76 /**
77 * Add another frame to the Desktop pane
78 */
79 public void addFrame(JarlShellFrame jarlShellFrame) {
80 Component component = jarlShellFrame.getComponent();
81 m_desktop.add(component);
82
83 int count = m_desktop.getAllFrames().length - 1;
84 component.setLocation(INTERNAL_OFFSET_X * count, INTERNAL_OFFSET_Y * count);
85 }
86
87 protected void shutDown() {
88 try {
89 m_jarlShell.saveModels();
90 } catch (Exception e1) {
91 e1.printStackTrace();
92 }
93 System.exit(0);
94 }
95
96 protected void initializePanes() {
97 m_mspJarlFile = new MSPJarlFile();
98 m_mspJarlFile.init(m_jarlShell);
99
100 m_mspFrameSlider = new MSPFrameSlider();
101 m_mspFrameSlider.init(m_jarlShell);
102
103 m_mspOptionsPane = new MSPOptionsPane();
104 m_mspOptionsPane.init(m_jarlShell);
105
106 m_mspRenderPane = new MSPRenderPane();
107 m_mspRenderPane.init(m_jarlShell);
108
109 m_mspRenderStatusPane = new MSPRenderStatusPane();
110 m_mspRenderStatusPane.init(m_jarlShell);
111
112 m_mspPreviewAnimationPane = new MSPPreviewAnimationPane();
113 m_mspPreviewAnimationPane.init(m_jarlShell);
114 }
115
116 protected JPanel createStatusPanel() {
117 GridBagLayout gridBagLayout = new GridBagLayout();
118 GridBagConstraints c = new GridBagConstraints();
119 c.fill = GridBagConstraints.HORIZONTAL;
120 c.anchor = GridBagConstraints.CENTER;
121
122 JPanel panel = new JPanel();
123 panel.setLayout(gridBagLayout);
124
125 //row 0 TextField + Button
126 Component topPane = m_mspJarlFile.createComponent();
127 c.gridx = 0;
128 c.gridy = 0;
129 c.weightx = 5;
130 c.weighty = 1;
131 c.gridwidth = 5;
132 gridBagLayout.setConstraints(topPane, c);
133 panel.add(topPane);
134
135 //row 2 Current Frame Slider
136 Component frameSlider = m_mspFrameSlider.createComponent();
137 c.gridx = 0;
138 c.gridy = 2;
139 c.weighty = 2;
140 gridBagLayout.setConstraints(frameSlider, c);
141 panel.add(frameSlider);
142
143 //row 3 Options & (Dimension pane)
144 Component optionPane = m_mspOptionsPane.createComponent();
145 c.gridx = 0;
146 c.gridy = 4;
147 c.weighty = 2;
148 gridBagLayout.setConstraints(optionPane, c);
149 panel.add(optionPane);
150
151 //row 4 render pane
152 Component renderPanel = m_mspRenderPane.createComponent();
153 c.gridx = 0;
154 c.gridy = 6;
155 c.weighty = 2;
156 gridBagLayout.setConstraints(renderPanel, c);
157 panel.add(renderPanel);
158
159 //row 5 render status
160 Component renderStatusPanel = m_mspRenderStatusPane.createComponent();
161 c.gridx = 0;
162 c.gridy = 8;
163 c.weighty = 2;
164 gridBagLayout.setConstraints(renderStatusPanel, c);
165 panel.add(renderStatusPanel);
166
167 //row 6 preview pane
168 Component previewAnimationPane = m_mspPreviewAnimationPane.createComponent();
169 c.gridx = 0;
170 c.gridy = 10;
171 c.weighty = 2;
172 gridBagLayout.setConstraints(previewAnimationPane, c);
173 panel.add(previewAnimationPane);
174
175 //row N
176 JLabel emptyLabel = new JLabel();
177 c.weighty = 20.0;
178 c.gridx = 0;
179 c.gridy = 99;
180 gridBagLayout.setConstraints(emptyLabel, c);
181 panel.add(emptyLabel);
182
183 m_topPane = topPane;
184 setFocus(m_topPane);
185
186 return panel;
187 }
188
189 protected void setFocus(Component component) {
190 component.requestFocus();
191 }
192
193 /**
194 * Create the scroll pane that contains the JDesktopPane
195 */
196 protected JScrollPane createScrollPane() {
197 m_desktop = new JDesktopPane(); //a specialized layered pane
198 m_desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); //Make dragging faster
199 JScrollPane scroller = new JScrollPane();
200 scroller.setViewportView(m_desktop);
201 return scroller;
202 }
203
204 /**
205 * Create the simple menu bar
206 */
207 protected JMenuBar createMenuBar() {
208 JMenuBar menuBar = new JMenuBar();
209
210 //file
211 JMenu menu = new JMenu("File");
212 menu.setMnemonic(KeyEvent.VK_F);
213 JMenuItem menuItem = new JMenuItem("Exit");
214 menuItem.setMnemonic(KeyEvent.VK_X);
215 menuItem.addActionListener(new ActionListener() {
216 public void actionPerformed(ActionEvent e) {
217 shutDown();
218 }
219 });
220 menu.add(menuItem);
221 menuBar.add(menu);
222
223 //window
224 menu = new JMenu("Window");
225 menu.setMnemonic(KeyEvent.VK_W);
226 menuItem = new JMenuItem("Cascade All");
227 menuItem.setMnemonic(KeyEvent.VK_C);
228 menuItem.addActionListener(new ActionListener() {
229 public void actionPerformed(ActionEvent e) {
230 cascadeDesktop();
231 setFocus(m_topPane);
232 }
233 });
234 menu.add(menuItem);
235 menuItem = new JMenuItem("Minimize All");
236 menuItem.setMnemonic(KeyEvent.VK_M);
237 menuItem.addActionListener(new ActionListener() {
238 public void actionPerformed(ActionEvent e) {
239 minimizeDesktop();
240 setFocus(m_topPane);
241 }
242 });
243 menu.add(menuItem);
244 menuItem = new JMenuItem("Restore All");
245 menuItem.setMnemonic(KeyEvent.VK_R);
246 menuItem.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent e) {
248 maximizeDesktop();
249 setFocus(m_topPane);
250 }
251 });
252 menu.add(menuItem);
253 menuItem = new JMenuItem("Close All");
254 menuItem.setMnemonic(KeyEvent.VK_L);
255 menuItem.addActionListener(new ActionListener() {
256 public void actionPerformed(ActionEvent e) {
257 closeDesktop();
258 setFocus(m_topPane);
259 }
260 });
261 menu.add(menuItem);
262
263 menuBar.add(menu);
264 return menuBar;
265 }
266
267 /**
268 * An adapter for the baseView
269 */
270 public static class MainFrameView extends BaseView {
271
272 public String[] getModelsToSubscribe() {
273 return new String[]{ViewPrefModel.class.getName(), JarlContextModel.class.getName()};
274 }
275
276 public void onChange(BaseModel baseModel, String propertyName) {
277 MainJarlShellFrame frame = m_jarlShell.getMainJarlShellFrame();
278 if (frame != null) {
279 frame.onChange(baseModel, propertyName);
280 }
281 }
282 }
283
284 /**
285 * Foward the changes on
286 */
287 public void onChange(BaseModel baseModel, String propertyName) {
288 m_mspJarlFile.onChange(baseModel, propertyName);
289 m_mspFrameSlider.onChange(baseModel, propertyName);
290 m_mspOptionsPane.onChange(baseModel, propertyName);
291 m_mspRenderPane.onChange(baseModel, propertyName);
292 m_mspRenderStatusPane.onChange(baseModel, propertyName);
293 }
294
295 protected void closeDesktop() {
296 Component[] comps = m_desktop.getComponents();
297 for (int index = 0; index < comps.length; index++) {
298 Component comp = comps[index];
299 if (comp instanceof JInternalFrame) {
300 ((JInternalFrame) comp).dispose();
301 } else if (comp instanceof JInternalFrame.JDesktopIcon) {
302 ((JInternalFrame.JDesktopIcon) comp).getInternalFrame().dispose();
303 }
304 }
305 }
306
307 protected void minimizeDesktop() {
308 Component[] comps = m_desktop.getComponents();
309 for (int index = 0; index < comps.length; index++) {
310 Component comp = comps[index];
311 if (comp instanceof JInternalFrame) {
312 try {
313 ((JInternalFrame) comp).setIcon(true);
314 } catch (PropertyVetoException e) {
315 e.printStackTrace();
316 }
317 }
318 }
319 }
320
321 protected void maximizeDesktop() {
322 Component[] comps = m_desktop.getComponents();
323 for (int index = 0; index < comps.length; index++) {
324 Component comp = comps[index];
325 try {
326 if (comp instanceof JInternalFrame) {
327 ((JInternalFrame) comp).setIcon(false);
328 } else if (comp instanceof JInternalFrame.JDesktopIcon) {
329 ((JInternalFrame.JDesktopIcon) comp).getInternalFrame().setIcon(false);
330 }
331 } catch (PropertyVetoException e) {
332 e.printStackTrace();
333 }
334 }
335 }
336
337 protected void cascadeDesktop() {
338 maximizeDesktop(); //just to make sure
339 Component[] comps = m_desktop.getComponents();
340 int count = comps.length;
341 int nextX = 0;
342 int nextY = 0;
343
344 for (int i = count - 1; i >= 0; i--) {
345 Component comp = comps[i];
346 if (comp instanceof JInternalFrame) {
347 Dimension paneSize = getSize();
348 Rectangle bounds = comp.getBounds();
349 int targetWidth = bounds.width;
350 int targetHeight = bounds.height;
351
352 if (nextX + targetWidth > paneSize.width || nextY + targetHeight > paneSize.height) {
353 nextX = 0;
354 nextY = 0;
355 }
356
357 m_desktop.getDesktopManager().setBoundsForFrame((JComponent) comp, nextX, nextY, targetWidth, targetHeight);
358
359 nextX += INTERNAL_OFFSET_X;
360 nextY += INTERNAL_OFFSET_Y;
361 }
362 }
363 }
364 }