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;
028:
029: import java.util.Hashtable;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Enumeration;
033: import java.util.Vector;
034: import java.util.ArrayList;
035:
036: import javax.swing.JOptionPane;
037: import javax.swing.JPanel;
038: import javax.swing.JComboBox;
039: import javax.swing.JButton;
040: import javax.swing.JLabel;
041: import javax.swing.JComponent;
042: import javax.swing.Box;
043: import javax.swing.BoxLayout;
044: import java.awt.GridLayout;
045: import java.awt.GridBagLayout;
046: import java.awt.GridBagConstraints;
047: import java.awt.Insets;
048: import javax.swing.border.LineBorder;
049:
050: import java.awt.event.ItemEvent;
051: import java.awt.event.ActionEvent;
052: import java.awt.event.ActionListener;
053: import java.awt.event.ItemListener;
054:
055: import java.awt.Component;
056: import java.awt.BorderLayout;
057: import java.awt.Dimension;
058: import java.awt.FlowLayout;
059: import java.awt.Color;
060:
061: /**
062: * <pre>
063: *
064: * The InventorySelectionPanel is the part of the ui where the user
065: * selects the inventory to be displayed. Drop down lists of orgs,
066: * class, and asset name are there so that the user can select which
067: * inventory item to peruse. Upon hitting the org selection or submit button * the selection is broadcast via a InventorySelectionEvent to any
068: * InventorySelectionListeners who have subscibed. If there is only one
069: * class type than there is no supply type combo box displayed.
070: *
071: * @see InventoryUIFrame
072: * @see InventorySelectionEvent
073: * @see InventorySelectionListener
074: *
075: **/
076:
077: public class InventorySelectionPanel extends JPanel implements
078: ActionListener, ItemListener {
079:
080: protected final static Insets BLANK_INSETS = new Insets(0, 0, 0, 0);
081:
082: public static final String SUBMIT = "Submit";
083: public static final String ORGS = "Org";
084: public static final String SUPPLY_TYPES = "Class";
085: public static final String ITEMS = "Items";
086:
087: public static final String ORGS_ALL = InventorySelectionEvent.ORGS_ALL;
088: public static final String ORGS_NAV = InventorySelectionEvent.ORGS_NAV;
089: public static final String ORGS_HIST = InventorySelectionEvent.ORGS_HIST;
090:
091: public static final String SUBMIT_TOOL_TIP = "Retrieves graph data for given organization/item";
092: public static final String ORG_TOOL_TIP = "Select Organization";
093: public static final String POP_TOOL_TIP = "Select Org Combo box population method";
094: public static final String SUPPLY_TOOL_TIP = "Select Class of Supply to filter items";
095: public static final String ASSET_TOOL_TIP = "Select Graph data inventory item";
096:
097: public static final String[] ORG_POP_OPTIONS = { ORGS_NAV,
098: ORGS_HIST, ORGS_ALL };
099:
100: protected ArrayList invListeners;
101:
102: protected JComboBox orgsBox;
103: protected JComboBox supplyTypesBox;
104: protected JComboBox assetNamesBox;
105:
106: protected JComboBox orgsPopulationBox;
107:
108: JButton submitButton;
109:
110: protected String currOrg;
111: protected String currSupplyType;
112: protected String currAssetName;
113:
114: JLabel supplyTypesLabel;
115:
116: Component parent;
117: boolean supplyTypesActive;
118: boolean supplyTypesVisible;
119:
120: public InventorySelectionPanel(Component aParent) {
121: orgsBox = new JComboBox();
122: assetNamesBox = new JComboBox();
123: supplyTypesBox = new JComboBox();
124: orgsPopulationBox = new JComboBox(ORG_POP_OPTIONS);
125: invListeners = new ArrayList();
126: parent = aParent;
127: supplyTypesActive = true;
128: supplyTypesVisible = true;
129: initPanel();
130: this .setVisible(true);
131: }
132:
133: public InventorySelectionPanel(Component aParent, Vector orgs,
134: String[] supplyTypes) {
135: orgsBox = new JComboBox();
136: assetNamesBox = new JComboBox();
137: supplyTypesBox = new JComboBox();
138: orgsPopulationBox = new JComboBox(ORG_POP_OPTIONS);
139: invListeners = new ArrayList();
140: parent = aParent;
141: initializeComboBoxes(orgs, supplyTypes);
142: initPanel();
143: this .setVisible(true);
144: }
145:
146: private void initPanel() {
147:
148: JButton submitButton = new JButton(SUBMIT);
149:
150: submitButton.setToolTipText(SUBMIT_TOOL_TIP);
151: orgsBox.setToolTipText(ORG_TOOL_TIP);
152: orgsPopulationBox.setToolTipText(POP_TOOL_TIP);
153: supplyTypesBox.setToolTipText(SUPPLY_TOOL_TIP);
154: assetNamesBox.setToolTipText(ASSET_TOOL_TIP);
155:
156: GridBagConstraints constraints;
157:
158: this .setBorder(new LineBorder(Color.blue));
159: assetNamesBox.setMinimumSize(new Dimension(450, 25));
160: orgsBox.setMinimumSize(new Dimension(100, 25));
161: //supplyTypesBox.setPreferredSize(new Dimension(100,25));
162: this .setLayout(new GridBagLayout());
163:
164: JPanel orgsPanel = new JPanel();
165: orgsPanel.setLayout(new FlowLayout());
166:
167: orgsBox.addActionListener(this );
168: constraints = new GridBagConstraints(
169: GridBagConstraints.RELATIVE, 0, 1, 1, 0.2, 1.0,
170: GridBagConstraints.WEST, GridBagConstraints.NONE,
171: BLANK_INSETS, 0, 0);
172: //orgsPanel.add(new JLabel(ORGS),constraints);
173: constraints = new GridBagConstraints(
174: GridBagConstraints.RELATIVE, 0, 6, 1, 0.8, 1.0,
175: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
176: BLANK_INSETS, 0, 0);
177: //orgsPanel.add(orgsBox,constraints);
178: orgsPanel.add(new JLabel(ORGS));
179: orgsPanel.add(orgsBox, constraints);
180:
181: JPanel supplyPanel = new JPanel();
182: supplyPanel.setLayout(new FlowLayout());
183:
184: supplyTypesBox.addItemListener(this );
185: supplyTypesLabel = new JLabel(SUPPLY_TYPES);
186: supplyTypesLabel.setVisible(supplyTypesVisible);
187: supplyTypesBox.setVisible(supplyTypesVisible);
188: supplyPanel.add(supplyTypesLabel);
189: supplyPanel.add(supplyTypesBox);
190:
191: JPanel orgsAndPop = new JPanel();
192: orgsAndPop.setLayout(new GridBagLayout());
193: orgsPopulationBox.addItemListener(this );
194: orgsPopulationBox.setSelectedItem(ORGS_NAV);
195: constraints = new GridBagConstraints(0,
196: GridBagConstraints.RELATIVE, 1, 1, 1.0, 1.0,
197: GridBagConstraints.WEST, GridBagConstraints.NONE,
198: BLANK_INSETS, 0, 0);
199:
200: orgsAndPop.add(orgsPanel, constraints);
201: orgsAndPop.add(orgsPopulationBox, constraints);
202:
203: assetNamesBox.addItemListener(this );
204: JPanel assetsPanel = makeAssetsPanel(assetNamesBox, ITEMS);
205: //assetsPanel.setBorder(new LineBorder(Color.black));
206:
207: submitButton.setPreferredSize(new Dimension(100, 25));
208: //submitButton.setMaximumSize(new Dimension(100,25));
209: submitButton.addActionListener(this );
210:
211: JPanel buttonPanel = new JPanel();
212: buttonPanel.setLayout(new FlowLayout());
213: //buttonPanel.setBorder(new LineBorder(Color.black));
214: //buttonPanel.add(Box.createHorizontalStrut(60));
215: buttonPanel.add(submitButton);
216: //buttonPanel.add(Box.createHorizontalStrut(100));
217:
218: /***
219:
220: JPanel buttonAndSupplyPanel = new JPanel();
221: buttonAndSupplyPanel.setLayout(new FlowLayout());
222: buttonAndSupplyPanel.setBorder(new LineBorder(Color.black));
223: buttonAndSupplyPanel.add(supplyPanel);
224: buttonAndSupplyPanel.add(buttonPanel);
225: ****/
226:
227: JPanel assetsAndButton = new JPanel();
228: assetsAndButton.setLayout(new GridBagLayout());
229:
230: constraints = new GridBagConstraints(0, 0, 8, 1, 1.0, 1.0,
231: GridBagConstraints.WEST, GridBagConstraints.NONE,
232: BLANK_INSETS, 0, 0);
233:
234: assetsAndButton.add(assetsPanel, constraints);
235: //assetsAndButton.add(buttonAndSupplyPanel);
236:
237: constraints = new GridBagConstraints(0, 1, 1, 1, 0.12, 1.0,
238: GridBagConstraints.WEST, GridBagConstraints.NONE,
239: BLANK_INSETS, 0, 0);
240:
241: assetsAndButton.add(supplyPanel, constraints);
242:
243: constraints = new GridBagConstraints(
244: GridBagConstraints.RELATIVE, 1, 7, 1, 0.88, 1.0,
245: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
246: BLANK_INSETS, 0, 0);
247:
248: assetsAndButton.add(buttonPanel, constraints);
249:
250: constraints = new GridBagConstraints(
251: GridBagConstraints.RELATIVE, 0, 2, 1, 0.4, 1.0,
252: GridBagConstraints.WEST, GridBagConstraints.BOTH,
253: BLANK_INSETS, 0, 0);
254:
255: this .add(orgsAndPop, constraints);
256:
257: constraints = new GridBagConstraints(
258: GridBagConstraints.RELATIVE, 0, 3, 1, 0.6, 1.0,
259: GridBagConstraints.WEST, GridBagConstraints.NONE,
260: BLANK_INSETS, 0, 0);
261:
262: this .add(assetsAndButton, constraints);
263:
264: this .setMaximumSize(new Dimension(780, 80));
265: }
266:
267: protected JComboBox getAssetNamesBox() {
268: return assetNamesBox;
269: }
270:
271: protected JPanel makeAssetsPanel(JComboBox assetNamesComboBox,
272: String itemsLabel) {
273: JPanel assetsPanel = new JPanel();
274: assetsPanel.setLayout(new FlowLayout());
275: assetsPanel.add(new JLabel(itemsLabel));
276: assetsPanel.add(assetNamesComboBox);
277: return assetsPanel;
278: }
279:
280: public void initializeComboBoxes(Vector orgs, String[] supplyTypes) {
281:
282: orgsBox.removeActionListener(this );
283: supplyTypesBox.removeItemListener(this );
284:
285: setOrganizations(orgs);
286: setSupplyTypes(supplyTypes);
287: setAssetNames(new Vector());
288:
289: orgsBox.addActionListener(this );
290: supplyTypesBox.addItemListener(this );
291:
292: //MWD this should be put back in once servlet plugin is
293: //is the default.
294: //fireInventorySelectionEvent(InventorySelectionEvent.ORG_SELECT);
295: }
296:
297: public void reinitializeOrgBox(Vector orgs) {
298:
299: orgsBox.removeActionListener(this );
300:
301: String origOrg = currOrg;
302: setOrganizations(orgs);
303: orgsBox.setSelectedItem(origOrg);
304: currOrg = (String) orgsBox.getSelectedItem();
305: if (((currOrg == null) && (origOrg != null))
306: || ((currOrg != null) && !(currOrg.equals(origOrg)))) {
307: //System.out.println("InventorySelectionPanel>>reinitializeOrgBox - So |" + currOrg + "| is different from |" + origOrg);
308: fireInventorySelectionEvent(InventorySelectionEvent.ORG_SELECT);
309: }
310: orgsBox.addActionListener(this );
311:
312: }
313:
314: public void setSupplyTypes(String[] supplyTypes) {
315: if ((supplyTypes == null) || (supplyTypes.length == 0)) {
316: currSupplyType = null;
317: supplyTypesActive = false;
318: supplyTypesVisible = false;
319: }
320: // if(supplyTypes.length == 1) {
321: // currSupplyType = supplyTypes[0];
322: // supplyTypesActive = false;
323: // supplyTypesVisible = true;
324: //}
325: else {
326: supplyTypesBox.removeAllItems();
327: for (int i = 0; i < supplyTypes.length; i++) {
328: String supplyType = supplyTypes[i];
329: supplyTypesBox.addItem(supplyType);
330: }
331: currSupplyType = (String) supplyTypesBox.getItemAt(0);
332: if (supplyTypes.length == 1) {
333: supplyTypesActive = false;
334: } else {
335: supplyTypesActive = true;
336: }
337: supplyTypesVisible = true;
338: }
339: supplyTypesLabel.setVisible(supplyTypesVisible);
340: supplyTypesBox.setVisible(supplyTypesVisible);
341: supplyTypesBox.setEnabled(supplyTypesActive);
342: }
343:
344: public void setOrganizations(Vector orgs) {
345: orgsBox.removeAllItems();
346: currOrg = null;
347: for (int i = 0; i < orgs.size(); i++) {
348: String orgName = (String) orgs.elementAt(i);
349: orgsBox.addItem(orgName);
350: }
351: if (orgs.size() > 1) {
352: currOrg = (String) orgsBox.getItemAt(0);
353: }
354: }
355:
356: public void setSelectedOrgAsset(String org, String assetName) {
357: assetNamesBox.removeItemListener(this );
358: orgsBox.removeActionListener(this );
359: orgsBox.setSelectedItem(org);
360: assetNamesBox.setSelectedItem(assetName);
361: currOrg = org;
362: currAssetName = assetName;
363: assetNamesBox.addItemListener(this );
364: orgsBox.addActionListener(this );
365: }
366:
367: public void setSelectedOrg(String org) {
368: orgsBox.setSelectedItem(org);
369: }
370:
371: public void setAssetNames(Vector assets) {
372: assetNamesBox.removeItemListener(this );
373: assetNamesBox.removeAllItems();
374: int currAssetIndex = -1;
375: for (int i = 0; i < assets.size(); i++) {
376: String assetName = (String) assets.elementAt(i);
377: assetNamesBox.addItem(assetName);
378: if (assetName.equals(currAssetName)) {
379: currAssetIndex = i;
380: }
381: }
382: if (currAssetIndex >= 0) {
383: assetNamesBox.setSelectedIndex(currAssetIndex);
384: } else if (assets.size() >= 1) {
385: currAssetName = (String) assetNamesBox.getItemAt(0);
386: } else {
387: currAssetName = null;
388: }
389: assetNamesBox.addItemListener(this );
390: }
391:
392: public void itemStateChanged(ItemEvent e) {
393: //System.out.println("Item State Changed event: " + e);
394: //System.out.println("Item id: " + e.getID() + " and SELECTED is:" + e.SELECTED);
395: if (e.getStateChange() == e.SELECTED) {
396: //System.out.println("Selected Event");
397: JComponent source = (JComponent) e.getSource();
398: if (source == orgsBox) {
399: currOrg = (String) orgsBox.getSelectedItem();
400: //System.out.println("Selected org " + currOrg);
401: fireInventorySelectionEvent(InventorySelectionEvent.ORG_SELECT);
402: } else if (source == supplyTypesBox) {
403: currSupplyType = (String) supplyTypesBox
404: .getSelectedItem();
405: // System.out.println("Selected supply type " + currSupplyType);
406: fireInventorySelectionEvent(InventorySelectionEvent.ORG_SELECT);
407: } else if (source == assetNamesBox) {
408: currAssetName = (String) assetNamesBox
409: .getSelectedItem();
410: // System.out.println("Selected asset " + currAssetName);
411: } else if (source == orgsPopulationBox) {
412: String currOrgPop = (String) getSelectedOrgPopMethod();
413:
414: /*** MWD take out until the right comboBoxEditor is in to do this
415: if(currOrgPop.equals(ORGS_NAV)) {
416: orgsBox.setEditable(false);
417: }
418: else {
419: orgsBox.setEditable(true);
420: }
421: ***/
422:
423: fireInventorySelectionEvent(InventorySelectionEvent.ORG_POP_SELECT);
424: }
425: }
426:
427: }
428:
429: public void actionPerformed(ActionEvent e) {
430: if (e.getActionCommand().equals(SUBMIT)) {
431: //System.out.println("Pressed Submit");
432: fireInventorySelectionEvent(InventorySelectionEvent.INVENTORY_SELECT);
433: } else if (e.getSource() == orgsBox) {
434: //System.out.println("InventorySelectionPanel: Orgs box action is: " + e);
435: currOrg = (String) orgsBox.getSelectedItem();
436: //This is fine if there is already an org selected from previous navigation but
437: // if you haven't then you'll get an NPE
438: if (currOrg == null) {
439: currOrg = ".";
440: }
441: fireInventorySelectionEvent(InventorySelectionEvent.ORG_SELECT);
442: }
443: }
444:
445: private static void displayErrorString(String reply) {
446: JOptionPane.showMessageDialog(null, reply, reply,
447: JOptionPane.ERROR_MESSAGE);
448: }
449:
450: public void addInventorySelectionListener(
451: InventorySelectionListener l) {
452: invListeners.add(l);
453: }
454:
455: public void removeInventorySelectionListener(
456: InventorySelectionListener l) {
457: invListeners.remove(l);
458: }
459:
460: protected void fireInventorySelectionEvent(int id) {
461: InventorySelectionEvent e = new InventorySelectionEvent(id,
462: this , currOrg, currSupplyType, currAssetName,
463: getSelectedOrgPopMethod());
464: for (int i = 0; i < invListeners.size(); i++) {
465: InventorySelectionListener l = (InventorySelectionListener) invListeners
466: .get(i);
467: l.selectionChanged(e);
468: }
469: }
470:
471: public void setSelectedOrgPopMethod(String orgPopMethod) {
472: orgsPopulationBox.removeItemListener(this );
473: orgsPopulationBox.setSelectedItem(orgPopMethod);
474: orgsPopulationBox.addItemListener(this );
475: }
476:
477: public String getSelectedOrg() {
478: return currOrg;
479: }
480:
481: public String getSelectedSupplyType() {
482: return currSupplyType;
483: }
484:
485: public String getSelectedAssetName() {
486: return currAssetName;
487: }
488:
489: public String getSelectedOrgPopMethod() {
490: return (String) orgsPopulationBox.getSelectedItem();
491: }
492:
493: }
|