1 |
|
---|
2 | /*
|
---|
3 | * CSFrame.java
|
---|
4 | * Copyright (C) 2001 New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | */
|
---|
20 | package org.nzdl.gsdl.SimpleGraphicalClient;
|
---|
21 |
|
---|
22 |
|
---|
23 | import javax.swing.JMenuItem;
|
---|
24 | import javax.swing.JMenuBar;
|
---|
25 | import javax.swing.JPanel;
|
---|
26 | import javax.swing.JFrame;
|
---|
27 | import javax.swing.JList;
|
---|
28 | import javax.swing.JPopupMenu;
|
---|
29 | import javax.swing.BoxLayout;
|
---|
30 | import javax.swing.JMenu;
|
---|
31 | import javax.swing.AbstractAction;
|
---|
32 | import javax.swing.SwingUtilities;
|
---|
33 | import javax.swing.KeyStroke;
|
---|
34 |
|
---|
35 | import java.awt.*;
|
---|
36 | import java.awt.event.*;
|
---|
37 | import java.util.Observer;
|
---|
38 | import java.util.Observable;
|
---|
39 | /**
|
---|
40 | * Class
|
---|
41 | *
|
---|
42 | * A class to
|
---|
43 | *
|
---|
44 | * @author Dave Nichols ([email protected])
|
---|
45 | * @author stuart yeates ([email protected])
|
---|
46 | * @version $Revision: 2222 $
|
---|
47 | * @see org.nzdl.gsdl.service.SimpleGraphicalClient.SimpleGraphicalClient
|
---|
48 | */
|
---|
49 |
|
---|
50 | public class CSFrame extends JFrame implements Observer, Constants
|
---|
51 |
|
---|
52 | {
|
---|
53 |
|
---|
54 | CSModel csModel;
|
---|
55 | SearchPanel searchPanel;
|
---|
56 | JPanel otherPanel;
|
---|
57 |
|
---|
58 |
|
---|
59 | // Constructor
|
---|
60 | public CSFrame(String title, CSModel newCSModel)
|
---|
61 | {
|
---|
62 | setTitle(title); // Set the window title
|
---|
63 |
|
---|
64 | csModel = newCSModel;
|
---|
65 | Container content = getContentPane();
|
---|
66 |
|
---|
67 | setJMenuBar(menuBar); // Add the menu bar to the window
|
---|
68 |
|
---|
69 | JMenu fileMenu = new JMenu("File"); // Create File menu
|
---|
70 | JMenu editMenu = new JMenu("Edit"); // Create Edit menu
|
---|
71 |
|
---|
72 | // File Menu
|
---|
73 |
|
---|
74 | //fileMenu.addSeparator();
|
---|
75 | fileMenu.add( quitAction = new FileAction("Poll Server", POLL_ID, this)).setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));
|
---|
76 | fileMenu.add( quitAction = new FileAction("Collect Garbage", GARBAGE_ID, this)).setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
|
---|
77 | fileMenu.add( quitAction = new FileAction("Quit", QUIT_ID, this)).setAccelerator(KeyStroke.getKeyStroke('Q', Event.CTRL_MASK));
|
---|
78 |
|
---|
79 | // Edit Menu
|
---|
80 |
|
---|
81 | editMenu.add( prefAction = new EditAction("Preferences", PREF_ID, this));
|
---|
82 |
|
---|
83 |
|
---|
84 | menuBar.add(fileMenu); // Add the file menu
|
---|
85 | menuBar.add(editMenu); // Add the edit menu
|
---|
86 |
|
---|
87 |
|
---|
88 | searchPanel = new SearchPanel(csModel, this);
|
---|
89 |
|
---|
90 | content.setLayout(new BoxLayout(content, BoxLayout.X_AXIS));
|
---|
91 | content.add(searchPanel);
|
---|
92 |
|
---|
93 | addWindowListener(new WindowHandler());
|
---|
94 |
|
---|
95 | pack();
|
---|
96 | searchPanel.searchTextField.requestFocus();
|
---|
97 | } //end constructor
|
---|
98 |
|
---|
99 |
|
---|
100 | public void update(Observable observedThing, Object o )
|
---|
101 | {
|
---|
102 | System.out.println("Updating...");
|
---|
103 | //jlist.setListData( ((CSModel)observedThing).getModel() );
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 | // action objects
|
---|
109 |
|
---|
110 | abstract class CSAction extends AbstractAction {
|
---|
111 | int menuItemID;
|
---|
112 | CSFrame frame;
|
---|
113 |
|
---|
114 | CSAction( String name, int newMenuItemID, CSFrame f)
|
---|
115 | {
|
---|
116 | super(name);
|
---|
117 | menuItemID = newMenuItemID;
|
---|
118 | frame = f;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public abstract void actionPerformed(ActionEvent e);
|
---|
122 |
|
---|
123 | } //end CSAction
|
---|
124 |
|
---|
125 | class FileAction extends CSAction
|
---|
126 | {
|
---|
127 | FileAction(String name, int newMenuItemID, CSFrame frame)
|
---|
128 | {
|
---|
129 | super(name,newMenuItemID, frame );
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void actionPerformed(ActionEvent e)
|
---|
133 | {
|
---|
134 | switch (menuItemID) {
|
---|
135 | case GARBAGE_ID:
|
---|
136 | System.err.println("forcing gc...");
|
---|
137 | System.gc() ;
|
---|
138 | System.runFinalization();
|
---|
139 | System.err.println("forcing gc...");
|
---|
140 | System.gc() ;
|
---|
141 | System.runFinalization();
|
---|
142 | System.err.println("forced gc");
|
---|
143 | break;
|
---|
144 | case POLL_ID:
|
---|
145 | System.out.println("poll chosen from menu");
|
---|
146 | break;
|
---|
147 | case QUIT_ID:
|
---|
148 | System.out.println("quit chosen from menu");
|
---|
149 | dispose(); // free resources
|
---|
150 | System.exit(0); // exit program
|
---|
151 | break;
|
---|
152 | default:
|
---|
153 | System.out.println("unimplemented option chosen from FileAction");
|
---|
154 | break;
|
---|
155 | } // end switch
|
---|
156 | }//end actionPerformed
|
---|
157 | } // end FileAction
|
---|
158 |
|
---|
159 | class EditAction extends CSAction
|
---|
160 | {
|
---|
161 | EditAction(String name, int newMenuItemID, CSFrame frame)
|
---|
162 | {
|
---|
163 | super(name,newMenuItemID, frame );
|
---|
164 | }
|
---|
165 |
|
---|
166 | public void actionPerformed(ActionEvent e)
|
---|
167 | {
|
---|
168 | switch (menuItemID) {
|
---|
169 | case PREF_ID:
|
---|
170 | PreferencesDialog prefDialog = new PreferencesDialog(frame, "Preferences", false); // make this the instance of CSFrame
|
---|
171 | break;
|
---|
172 | default:
|
---|
173 | System.out.println("unimplemented option chosen from EditAction");
|
---|
174 | break;
|
---|
175 | } // end switch
|
---|
176 | } //end actionPerformed
|
---|
177 | } // end EditAction
|
---|
178 |
|
---|
179 |
|
---|
180 | class WindowHandler extends WindowAdapter
|
---|
181 | {
|
---|
182 |
|
---|
183 | public void windowOpened(WindowEvent e) {
|
---|
184 |
|
---|
185 | SwingUtilities.invokeLater(new Runnable() {
|
---|
186 | public void run() {
|
---|
187 | searchPanel.searchTextField.requestFocus();
|
---|
188 | searchPanel.searchTextField.setCaretPosition(
|
---|
189 | searchPanel.searchTextField.getText().length() );
|
---|
190 | searchPanel.searchTextField.selectAll();
|
---|
191 | }
|
---|
192 | });
|
---|
193 | } //end windowOpened
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | public void windowClosing(WindowEvent e)
|
---|
198 | {
|
---|
199 | dispose(); // free resources
|
---|
200 | System.exit(0); // exit program
|
---|
201 | }
|
---|
202 | }// end WindowHandler
|
---|
203 |
|
---|
204 |
|
---|
205 | class MouseHandler extends MouseAdapter
|
---|
206 | {
|
---|
207 | private Frame frame;
|
---|
208 |
|
---|
209 | public MouseHandler( CSFrame f )
|
---|
210 | {
|
---|
211 | this.frame = f;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public void mouseClicked(MouseEvent e) {
|
---|
215 | //System.out.println("mouse event" );
|
---|
216 | if (e.getClickCount() == 2) {
|
---|
217 | System.out.println("double click - mouse event" );
|
---|
218 | int index = jlist.locationToIndex(e.getPoint());
|
---|
219 | System.out.println("index is " + index );
|
---|
220 | //psObject = (PeopleSpaceObject) jlist.getModel().getElementAt(index);
|
---|
221 | } //end if
|
---|
222 | } //end mouseClicked
|
---|
223 |
|
---|
224 | public void mousePressed(MouseEvent e) {
|
---|
225 | maybeShowPopup(e);
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void mouseReleased(MouseEvent e) {
|
---|
229 | maybeShowPopup(e);
|
---|
230 | }
|
---|
231 |
|
---|
232 | private void maybeShowPopup(MouseEvent e) {
|
---|
233 | if (e.isPopupTrigger()) {
|
---|
234 | contextPopup.show(e.getComponent(),
|
---|
235 | e.getX(), e.getY());
|
---|
236 | } //end if
|
---|
237 | } //end maybeShowPopup
|
---|
238 |
|
---|
239 | } //end class MouseHandler
|
---|
240 |
|
---|
241 | // private declarations
|
---|
242 | private JMenuBar menuBar = new JMenuBar(); // Window menu bar
|
---|
243 | private JMenuItem quitItem;
|
---|
244 |
|
---|
245 | private JList jlist, peopleList;
|
---|
246 | private JPopupMenu contextPopup;
|
---|
247 |
|
---|
248 | // FileActions
|
---|
249 | private FileAction newSpaceAction, newGroupAction, newPersonAction, quitAction;
|
---|
250 |
|
---|
251 | // EditActions
|
---|
252 |
|
---|
253 | private EditAction prefAction;
|
---|
254 |
|
---|
255 | //Dialog
|
---|
256 |
|
---|
257 | private PreferencesDialog newPreferencesDialog;
|
---|
258 |
|
---|
259 | } //end CSFrame
|
---|