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.mlm.plugin;
028:
029: import java.awt.Component;
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.Insets;
033: import java.awt.Rectangle;
034: import java.io.BufferedReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.InputStreamReader;
038: import java.io.StreamTokenizer;
039: import java.util.HashMap;
040:
041: import javax.swing.JComponent;
042: import javax.swing.JFrame;
043: import javax.swing.JLabel;
044: import javax.swing.JPanel;
045:
046: import org.cougaar.util.ConfigFinder;
047:
048: public class UICoordinator {
049: static HashMap locations = new HashMap();
050:
051: private String name;
052:
053: private Rectangle bounds;
054:
055: static {
056: try {
057: doFileLayout(System.getProperty(
058: "org.cougaar.core.plugin.UICoordinator.ui_config",
059: "ui_config.txt"));
060: } catch (IOException e) {
061: doDefaultLayout();
062: }
063: }
064:
065: /**
066: * The layout found in a file.
067: **/
068: private static void doFileLayout(String filename)
069: throws IOException {
070:
071: InputStream fs = ConfigFinder.getInstance().open(filename);
072: BufferedReader reader = new BufferedReader(
073: new InputStreamReader(fs));
074: try {
075: StreamTokenizer tokens = new StreamTokenizer(reader);
076: tokens.commentChar('#');
077: tokens.whitespaceChars(',', ',');
078: while (true) {
079: int ttype = tokens.nextToken();
080: if (ttype == tokens.TT_EOF)
081: return;
082: if (ttype != '"' && ttype != '\''
083: && ttype != tokens.TT_WORD)
084: throw new IOException("Bad file format");
085: String title = tokens.sval;
086: if (tokens.nextToken() != tokens.TT_NUMBER)
087: throw new IOException("Bad file format");
088: int x = (int) tokens.nval;
089: if (tokens.nextToken() != tokens.TT_NUMBER)
090: throw new IOException("Bad file format");
091: int y = (int) tokens.nval;
092: if (tokens.nextToken() != tokens.TT_NUMBER)
093: throw new IOException("Bad file format");
094: int width = (int) tokens.nval;
095: if (tokens.nextToken() != tokens.TT_NUMBER)
096: throw new IOException("Bad file format");
097: int height = (int) tokens.nval;
098: add(title, x, y, width, height);
099: }
100: } finally {
101: reader.close();
102: }
103: }
104:
105: /**
106: * Use this layout if a config file cannot be found.
107: **/
108: private static void doDefaultLayout() {
109: add("GLSInitPlugin", 0, 0, 378, 78);
110: add("GLSRescindPlugin", 0, 80, 378, 78);
111: add("OplanPlugin", 0, 160, 378, 78);
112: add("AllocationAssessorPlugin for <3ID>", 0, 240, 378, 400);
113: add("MCCTriggerCreatorPlugin", 380, 0, 378, 78);
114: add("PolicyPlugin", 380, 80, 378, 108);
115: add("AllocationAssessorPlugin for <3-69-ARBN>", 380, 160, 378,
116: 400);
117: }
118:
119: private UICoordinator(String name, int x, int y, int width,
120: int height) {
121: this .name = name;
122: this .bounds = new Rectangle(x, y, width, height);
123: }
124:
125: public static UICoordinator add(String name, int x, int y,
126: int width, int height) {
127: UICoordinator uic = new UICoordinator(name, x, y, width, height);
128: locations.put(name, uic);
129: return uic;
130: }
131:
132: public static void setBounds(JFrame frame) {
133: UICoordinator uic = (UICoordinator) locations.get(frame
134: .getTitle());
135: if (uic == null) {
136: return;
137: }
138: frame.setBounds(uic.bounds);
139: }
140:
141: private static GridBagConstraints buttonConstraints = new GridBagConstraints();
142: private static GridBagConstraints labelConstraints = new GridBagConstraints();
143: private static GridBagConstraints row2Constraints = new GridBagConstraints();
144: static {
145: buttonConstraints.gridx = 0;
146: buttonConstraints.gridy = GridBagConstraints.RELATIVE;
147: buttonConstraints.fill = GridBagConstraints.NONE;
148: buttonConstraints.weightx = 0.0;
149: buttonConstraints.anchor = GridBagConstraints.CENTER;
150: buttonConstraints.insets = new Insets(2, 10, 2, 10);
151:
152: labelConstraints.gridx = 1;
153: labelConstraints.gridy = buttonConstraints.gridy;
154: labelConstraints.fill = GridBagConstraints.HORIZONTAL;
155: labelConstraints.weightx = 1.0;
156: labelConstraints.anchor = GridBagConstraints.CENTER;
157: labelConstraints.insets = new Insets(2, 10, 2, 10);
158:
159: row2Constraints.gridx = 0;
160: row2Constraints.gridy = GridBagConstraints.RELATIVE;
161: row2Constraints.gridwidth = 2;
162: row2Constraints.fill = GridBagConstraints.HORIZONTAL;
163: row2Constraints.weightx = 1.0;
164: row2Constraints.anchor = GridBagConstraints.CENTER;
165: row2Constraints.insets = new Insets(2, 10, 2, 10);
166: }
167:
168: public static void layoutButton(JPanel panel, Component button) {
169: panel.setLayout(new GridBagLayout());
170: panel.add(button, buttonConstraints);
171: }
172:
173: public static void layoutButtonAndLabel(JPanel panel,
174: Component button, JComponent label) {
175: if (panel.getLayout() == null)
176: panel.setLayout(new GridBagLayout());
177: panel.add(button, buttonConstraints);
178: if (label instanceof JLabel) {
179: ((JLabel) label).setHorizontalAlignment(JLabel.RIGHT);
180: } else {
181: label.setAlignmentX(1f);
182: }
183: panel.add(label, labelConstraints);
184: }
185:
186: public static void layoutSecondRow(JPanel panel, Component c) {
187: panel.add(c, row2Constraints);
188: panel.revalidate();
189: }
190: }
|