001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.logistics.ui.inventory.dialog;
028:
029: import java.util.*;
030:
031: import javax.swing.JFrame;
032: import javax.swing.JMenuBar;
033: import javax.swing.JMenu;
034: import javax.swing.JMenuItem;
035: import javax.swing.JButton;
036: import javax.swing.JTextArea;
037: import javax.swing.JPanel;
038: import javax.swing.JDialog;
039: import javax.swing.JTabbedPane;
040: import javax.swing.JOptionPane;
041: import javax.swing.BorderFactory;
042: import javax.swing.JScrollPane;
043: import javax.swing.Box;
044: import javax.swing.JFileChooser;
045: import javax.swing.SwingUtilities;
046: import javax.swing.event.MouseInputAdapter;
047:
048: import java.awt.event.ActionListener;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.WindowEvent;
051: import java.awt.event.WindowAdapter;
052: import java.awt.*;
053: import java.awt.event.MouseEvent;
054: import java.awt.event.MouseListener;
055:
056: import java.io.File;
057: import java.io.FileWriter;
058: import java.io.FileReader;
059: import java.io.BufferedReader;
060: import java.io.IOException;
061:
062: import org.cougaar.util.log.Logging;
063: import org.cougaar.util.log.Logger;
064:
065: import org.cougaar.logistics.ui.inventory.data.InventoryPreferenceData;
066: import org.cougaar.logistics.ui.inventory.dialog.ColorTab;
067: import org.cougaar.logistics.ui.inventory.InventorySelectionListener;
068: import org.cougaar.logistics.ui.inventory.InventorySelectionEvent;
069: import org.cougaar.logistics.ui.inventory.InventoryUIFrame;
070: import org.cougaar.logistics.ui.inventory.InventoryMenuEvent;
071:
072: /**
073: * <pre>
074: *
075: * The InventoryPreferenceDialog is the Dialog used to configure the options for the
076: * the Inventory GUI. You can configure Labels, Colors, Start Up Preferences etc.
077: * The data is persisted from session to session so that the user can customize
078: * his/her Inventory GUI to run they want.
079: *
080: * @see org.cougaar.logistics.ui.inventory.InventoryUIFrame
081: *
082: **/
083:
084: public class InventoryPreferenceDialog extends JDialog implements
085: ActionListener, InventorySelectionListener {
086:
087: public static final String PREFERENCE_DIALOG_TITLE = "Preferences";
088: public static final String OK_LABEL = "OK";
089: public static final String CANCEL_LABEL = "Cancel";
090: public static final String APPLY_LABEL = "Apply";
091:
092: static final Cursor waitCursor = Cursor
093: .getPredefinedCursor(Cursor.WAIT_CURSOR);
094: static final Cursor defaultCursor = Cursor
095: .getPredefinedCursor(Cursor.DEFAULT_CURSOR);
096:
097: protected Container contentPane;
098:
099: protected JTextArea editPane;
100: protected JFileChooser fileChooser;
101:
102: protected InventoryUIFrame mainFrame;
103: protected InventoryPreferenceData origPrefData;
104: protected InventoryPreferenceData newPrefData;
105: protected Logger logger;
106:
107: protected JTabbedPane prefTabs;
108:
109: protected String cip;
110: protected String helpFileStr;
111: protected String defaultSavePrefPath;
112:
113: public InventoryPreferenceDialog(InventoryUIFrame invUIFrame,
114: InventoryPreferenceData data) {
115: super (invUIFrame, PREFERENCE_DIALOG_TITLE, true);
116: mainFrame = invUIFrame;
117: origPrefData = data;
118: newPrefData = (InventoryPreferenceData) data.clone();
119: initializePrefDialog();
120: }
121:
122: protected void initializePrefDialog() {
123:
124: logger = Logging.getLogger(this );
125:
126: contentPane = getRootPane().getContentPane();
127:
128: cip = System.getProperty("org.cougaar.install.path");
129:
130: if ((cip == null) || (cip.trim().equals(""))) {
131: logger
132: .error("org.cougaar.install.path is not defined in Command line");
133: } else {
134: helpFileStr = cip + File.separator + "albbn"
135: + File.separator + "doc" + File.separator
136: + "alinvgui" + File.separator + "index.htm";
137: }
138:
139: String baseDir = System.getProperty("org.cougaar.workspace");
140: if ((baseDir == null) || (baseDir.trim().equals(""))) {
141: baseDir = cip + File.separator + "workspace";
142: }
143: defaultSavePrefPath = baseDir + File.separator + "INVGUICSV";
144: File pathDirs = new File(defaultSavePrefPath);
145:
146: fileChooser = new JFileChooser(pathDirs);
147: // fills frame
148: doMyLayout();
149:
150: pack();
151: setSize(450, 350);
152:
153: Point mainLoc = mainFrame.getLocation();
154:
155: setLocation((int) mainLoc.getX()
156: + (int) this .getSize().getWidth() / 2, (int) mainLoc
157: .getY()
158: + (int) this .getSize().getHeight() / 2);
159:
160: //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
161: // set location to middle of the screen
162: //setLocation(screenSize.width / 2 - (int) this.getSize().getWidth() / 2,
163: // screenSize.height / 2 - (int) this.getSize().getHeight() / 2);
164: //setVisible(true);
165: }
166:
167: protected void doMyLayout() {
168: //getRootPane().setJMenuBar(makeMenus());
169:
170: prefTabs = new JTabbedPane();
171:
172: contentPane.add(prefTabs, BorderLayout.CENTER);
173: prefTabs.setPreferredSize(new Dimension(450, 350));
174:
175: JPanel editPanel = new JPanel();
176: editPanel.setLayout(new BorderLayout());
177:
178: JButton okButton = new JButton("OK");
179: JButton applyButton = new JButton("Apply");
180: JButton cancelButton = new JButton("Cancel");
181: okButton.addActionListener(this );
182: applyButton.addActionListener(this );
183: cancelButton.addActionListener(this );
184:
185: JPanel bottomPanel = new JPanel();
186: bottomPanel.setBorder(BorderFactory.createCompoundBorder(
187: BorderFactory.createEmptyBorder(3, 3, 3, 3),
188: bottomPanel.getBorder()));
189: bottomPanel.setLayout(new BorderLayout());
190: bottomPanel.add(Box.createHorizontalStrut(100),
191: BorderLayout.WEST);
192: JPanel buttonPanel = new JPanel();
193: buttonPanel.add(okButton);
194: buttonPanel.add(applyButton);
195: buttonPanel.add(cancelButton);
196: bottomPanel.add(buttonPanel, BorderLayout.EAST);
197:
198: prefTabs.add("Startup Prefs", new StartUpTab(mainFrame, this ,
199: newPrefData));
200: prefTabs.add("Color Prefs", new ColorTab(mainFrame, this ,
201: newPrefData));
202: prefTabs.add("Unit Prefs", new UnitPrefTab(mainFrame, this ,
203: newPrefData));
204:
205: //Handle window closing correctly.
206: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
207:
208: contentPane.add(bottomPanel, BorderLayout.SOUTH);
209:
210: }
211:
212: protected JMenuBar makeMenus() {
213: JMenuBar retval = new JMenuBar();
214:
215: JMenu file = new JMenu("File");
216: JMenuItem quit = new JMenuItem(InventoryMenuEvent.MENU_Exit);
217: JMenuItem save = new JMenuItem(InventoryMenuEvent.MENU_SaveXML);
218: JMenuItem open = new JMenuItem(InventoryMenuEvent.MENU_OpenXML);
219: quit.addActionListener(this );
220: save.addActionListener(this );
221: open.addActionListener(this );
222: file.add(quit);
223: file.add(open);
224: file.add(save);
225: retval.add(file);
226:
227: JMenu connection = new JMenu("Connection");
228: JMenuItem connect = new JMenuItem(
229: InventoryMenuEvent.MENU_Connect);
230: connect.addActionListener(this );
231: connection.add(connect);
232: retval.add(connection);
233:
234: JMenu helpMenu = new JMenu("Help");
235: JMenuItem helpItem = new JMenuItem(InventoryMenuEvent.MENU_Help);
236: helpItem.addActionListener(this );
237: helpMenu.add(helpItem);
238: //Help popup is not done yet.
239: //retval.add(helpMenu);
240:
241: return retval;
242: }
243:
244: public boolean notifyMainFrameOfChanges() {
245: boolean changes = false;
246: for (int i = 0; i < prefTabs.getTabCount(); i++) {
247: PreferenceDialogTab tab = (PreferenceDialogTab) prefTabs
248: .getComponentAt(i);
249: if (tab.prefsChanged()) {
250: tab.flushValuesToData();
251: changes = true;
252: }
253: }
254: if (changes) {
255: mainFrame.prefDataChanged(origPrefData, newPrefData);
256: origPrefData = (InventoryPreferenceData) newPrefData
257: .clone();
258: }
259:
260: return changes;
261: }
262:
263: public void actionPerformed(ActionEvent e) {
264: if (e.getActionCommand().equals(OK_LABEL)) {
265: notifyMainFrameOfChanges();
266: mainFrame.savePrefData(newPrefData);
267: setVisible(false);
268: } else if (e.getActionCommand().equals(CANCEL_LABEL)) {
269: setVisible(false);
270: } else if (e.getActionCommand().equals(APPLY_LABEL)) {
271: notifyMainFrameOfChanges();
272: }
273: }
274:
275: protected void popupHelpPage() {
276: }
277:
278: protected void saveXML() {
279: File pathDirs = new File(defaultSavePrefPath);
280: try {
281: if (!pathDirs.exists()) {
282: pathDirs.mkdirs();
283: }
284: } catch (Exception ex) {
285: logger.error("Error creating default directory "
286: + defaultSavePrefPath, ex);
287: }
288: if (newPrefData != null) {
289: String fileID = defaultSavePrefPath
290: + "InventoryUIPrefs.txt";
291: System.out.println("Save to file: " + fileID);
292: fileChooser.setSelectedFile(new File(fileID));
293: }
294: int retval = fileChooser.showSaveDialog(this );
295: if (retval == fileChooser.APPROVE_OPTION) {
296: File saveFile = fileChooser.getSelectedFile();
297: try {
298: FileWriter fw = new FileWriter(saveFile);
299: fw.write(editPane.getText());
300: fw.flush();
301: fw.close();
302: } catch (IOException ioe) {
303: throw new RuntimeException(ioe);
304: }
305: }
306:
307: }
308:
309: public static void timeConsumingTaskStart(Component c) {
310: JFrame frame = null;
311: if (c != null)
312: frame = (JFrame) SwingUtilities.getRoot(c);
313: if (frame == null)
314: return;
315: timeConsumingTaskStart(frame);
316: }
317:
318: public static void timeConsumingTaskStart(JFrame frame) {
319: Component glass = frame.getGlassPane();
320: glass.setCursor(waitCursor);
321: glass.addMouseListener(new DoNothingMouseListener());
322: glass.setVisible(true);
323: }
324:
325: public static void timeConsumingTaskEnd(Component c) {
326: JFrame frame = null;
327: if (c != null)
328: frame = (JFrame) SwingUtilities.getRoot(c);
329: if (frame == null)
330: return;
331: timeConsumingTaskEnd(frame);
332: }
333:
334: public static void timeConsumingTaskEnd(JFrame frame) {
335: Component glass = frame.getGlassPane();
336: MouseListener[] mls = (MouseListener[]) glass
337: .getListeners(MouseListener.class);
338: for (int i = 0; i < mls.length; i++)
339: if (mls[i] instanceof DoNothingMouseListener)
340: glass.removeMouseListener(mls[i]);
341: glass.setCursor(defaultCursor);
342: glass.setVisible(false);
343: }
344:
345: protected static void displayErrorString(String reply) {
346: JOptionPane.showMessageDialog(null, reply, reply,
347: JOptionPane.ERROR_MESSAGE);
348: }
349:
350: public void selectionChanged(InventorySelectionEvent e) {
351:
352: }
353:
354: protected class HelpDialog extends JDialog {
355:
356: protected String helpFileURLStr;
357:
358: public HelpDialog(JFrame owner, String aHelpFileURLStr) {
359: super (owner, "Inventory GUI Help", false);
360: helpFileURLStr = aHelpFileURLStr;
361: setupAndLayout();
362: }
363:
364: protected void setupAndLayout() {
365:
366: }
367:
368: }
369:
370: protected class TaskStartRunnable implements Runnable {
371:
372: InventoryUIFrame frame;
373:
374: public TaskStartRunnable(InventoryUIFrame aFrame) {
375: frame = aFrame;
376: }
377:
378: public void run() {
379: timeConsumingTaskStart(frame);
380: }
381: }
382:
383: static class DoNothingMouseListener extends MouseInputAdapter {
384: public void mouseClicked(MouseEvent e) {
385: e.consume();
386: }
387:
388: public void mouseDragged(MouseEvent e) {
389: e.consume();
390: }
391:
392: public void mouseEntered(MouseEvent e) {
393: e.consume();
394: }
395:
396: public void mouseExited(MouseEvent e) {
397: e.consume();
398: }
399:
400: public void mouseMoved(MouseEvent e) {
401: e.consume();
402: }
403:
404: public void mousePressed(MouseEvent e) {
405: e.consume();
406: }
407:
408: public void mouseReleased(MouseEvent e) {
409: e.consume();
410: }
411: }
412: }
|