001: package vicazh.hyperpool;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.*;
006: import java.util.logging.*;
007: import javax.swing.*;
008: import javax.swing.event.*;
009: import javax.swing.tree.*;
010: import org.jdesktop.jdic.tray.*;
011: import org.jdesktop.jdic.tray.internal.*;
012:
013: /**
014: * The windows manager
015: *
016: * @author Victor Zhigunov
017: * @version 0.4.0
018: */
019: public class IWindow implements ActionListener, TreeSelectionListener {
020:
021: private Container container;
022:
023: private String resource;
024:
025: private Reader reader;
026:
027: private JMenuItem menu;
028:
029: private AbstractButton button;
030:
031: private Map<Object, Window> map;
032:
033: private JTree tree;
034:
035: private Component component;
036:
037: private TrayIcon tray;
038:
039: private JMenuItem menuTray;
040:
041: private SystemTray systemTray;
042:
043: /**
044: * @param container
045: * manager container
046: * @param resource
047: * xml resource
048: * @param reader
049: * xml reader
050: * @param menu
051: * create menu
052: * @param button
053: * create button
054: * @param map
055: * frames map
056: * @param tree
057: * manager tree
058: * @param component
059: * application component
060: * @param trayIcon
061: * tray icon
062: * @param trayArg
063: * tray arg
064: * @param trayMenu
065: * tray menu
066: * @param status
067: * tray status
068: * @param menuTray
069: * tray menu item
070: */
071: public IWindow(Container container, String resource, Reader reader,
072: JMenuItem menu, AbstractButton button,
073: Map<Object, Window> map, JTree tree, Component component,
074: Icon trayIcon, String trayArg, JPopupMenu trayMenu,
075: boolean status, JMenuItem menuTray) {
076: this .container = container;
077: this .resource = resource;
078: this .reader = reader;
079: this .menu = menu;
080: menu.addActionListener(this );
081: this .button = button;
082: button.addActionListener(this );
083: this .map = map;
084: this .tree = tree;
085: tree.addTreeSelectionListener(this );
086: this .component = component;
087: try {
088: systemTray = SystemTray.getDefaultSystemTray();
089: tray = new TrayIcon(trayIcon, trayArg, trayMenu);
090: tray.addActionListener(this );
091: this .menuTray = menuTray;
092: menuTray.addActionListener(this );
093: tray(status);
094: } catch (Throwable e) {
095: menuTray.setEnabled(false);
096: component.setVisible(true);
097: }
098: valueChanged(null);
099: }
100:
101: public Component get() {
102: int count = container.getComponentCount();
103: for (int i = 0; i < count; i++) {
104: Component c = container.getComponent(i);
105: if (c.isVisible())
106: return c;
107: }
108: return null;
109: }
110:
111: private void tray(boolean b) {
112: component.setVisible(!b);
113: if (b)
114: systemTray.addTrayIcon(tray);
115: else
116: systemTray.removeTrayIcon(tray);
117: }
118:
119: public void actionPerformed(ActionEvent e) {
120: Object source = e.getSource();
121: if (source == menu || source == button) {
122: valueChanged(null);
123: try {
124: reader.getObject(resource);
125: } catch (Exception ex) {
126: Start.logger.log(Level.SEVERE, ex.getMessage(), ex);
127: }
128: } else if (source instanceof TrayIconService)
129: tray(false);
130: else if (source == menuTray)
131: tray(true);
132: else {
133: String command = e.getActionCommand();
134: if (command.equals("close"))
135: stop();
136: else {
137: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
138: .getLastSelectedPathComponent();
139: if (node == null)
140: return;
141: Window window = map.get(String.valueOf(node
142: .getUserObject().hashCode()));
143: if (window != null)
144: window.dispose();
145: }
146: }
147: }
148:
149: public void valueChanged(TreeSelectionEvent e) {
150: if (e != null) {
151: DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
152: .getLastSelectedPathComponent();
153: if (node != null) {
154: Object n = node.getUserObject();
155: if (n instanceof IElement) {
156: Component c = map.get(String.valueOf(n.hashCode()));
157: if (c == null) {
158: menu.setEnabled(true);
159: button.setEnabled(true);
160: return;
161: } else
162: c.setVisible(true);
163: }
164: }
165: }
166: menu.setEnabled(false);
167: button.setEnabled(false);
168: }
169:
170: public void stop() {
171: for (Window e : map.values())
172: e.dispose();
173: }
174: }
|