001: package org.osbl.client.wings.shell;
002:
003: import org.osbl.client.wings.shell.Window;
004: import org.osbl.client.action.ActionProvider;
005: import org.osbl.client.action.ChooserAction;
006: import org.wings.*;
007:
008: import javax.swing.*;
009: import java.util.*;
010: import java.util.List;
011: import java.awt.*;
012: import java.awt.event.ActionListener;
013: import java.awt.event.ActionEvent;
014:
015: public class NoWindow extends Window {
016: private List<SComponent> controls = new ArrayList<SComponent>();
017:
018: public NoWindow(SComponent contentPane) {
019: super (contentPane);
020: setTitleCode("navigation.mainWindow");
021: }
022:
023: public void refreshTitle() {
024: if (getContentPane() instanceof TitleProvider) {
025: TitleProvider titleProvider = (TitleProvider) getContentPane();
026: setTitleCode(titleProvider.getTitleCode());
027: setTitleArguments(titleProvider.getTitleArguments());
028: }
029: }
030:
031: public void refreshActions() {
032: controls.clear();
033: if (getContentPane() instanceof ButtonsProvider) {
034: ButtonsProvider buttonsProvider = (ButtonsProvider) getContentPane();
035: createControls(buttonsProvider.getActions());
036: }
037: setControls(new ArrayList<SComponent>(controls));
038: }
039:
040: public void refreshTools() {
041: List<Tool> toolsToBeShown = new LinkedList<Tool>();
042: boolean helpTool = false;
043:
044: SComponent shownComponent = getContentPane();
045:
046: if (shownComponent instanceof ToolsProvider) {
047: ToolsProvider toolsProvider = (ToolsProvider) shownComponent;
048: List<Tool> providersTools = toolsProvider.getTools();
049: if (providersTools != null) {
050: for (Iterator<Tool> iterator = providersTools
051: .iterator(); iterator.hasNext();) {
052: Tool tool = iterator.next();
053: if (tool instanceof ContextHelpTool)
054: helpTool = true;
055: toolsToBeShown.add(tool);
056: }
057: }
058: }
059:
060: // context help tool
061: if (!helpTool && shownComponent instanceof HelpProvider) {
062: HelpProvider helpProvider = (HelpProvider) shownComponent;
063: String helpCode = helpProvider.getHelpCode();
064: if (helpCode == null) {
065: throw new IllegalStateException(
066: "Help code of the help provider ("
067: + shownComponent.getClass().getName()
068: + ") is null, but shouldn't be");
069: }
070: ContextHelpTool contextHelpTool = Client.getInstance().contextHelpTool;
071: contextHelpTool.setHelpCode(helpCode);
072: toolsToBeShown.add(contextHelpTool);
073: }
074: setTools(toolsToBeShown);
075: }
076:
077: protected void createControls(List<Action> actions) {
078: ActionProvider actionProvider = client.getActionProvider();
079: String[] names = new String[actions.size()];
080: int i = 0;
081: for (Action action : actions) {
082: if (action == null) {
083: i++;
084: continue;
085: }
086: actionProvider.configure(action);
087: names[i++] = (String) action.getValue(Action.NAME);
088: }
089: SIcon[] icons = createActionBarButtonIcons(names);
090:
091: GridBagConstraints buttonConstraints = new GridBagConstraints();
092: buttonConstraints.weightx = .01;
093: buttonConstraints.gridwidth = 1;
094: buttonConstraints.insets = new Insets(0, 0, 0, 10);
095:
096: i = 0;
097: for (Action action : actions) {
098: if (action == null) {
099: controls.add(new SLabel("<html> "));
100: i++;
101: continue;
102: }
103:
104: if (action instanceof ChooserAction) {
105: controls
106: .add(createChooserActionComponent((ChooserAction) action));
107: } else {
108: SButton button = new SToolBarButton(action);
109: button.setStyle("XButton");
110: button.setText(null);
111: button.setIcon(icons[i * 2]);
112: button.setDisabledIcon(icons[i * 2 + 1]);
113: button.setToolTipText((String) action
114: .getValue(Action.SHORT_DESCRIPTION));
115: controls.add(button);
116: }
117: i++;
118: }
119: buttonConstraints.weightx = 1.0 - i * .01;
120: controls.add(new SLabel("<html> "));
121: }
122:
123: protected SComboBox createChooserActionComponent(
124: final ChooserAction chooserAction) {
125: final DefaultComboBoxModel model = new DefaultComboBoxModel(
126: chooserAction.getDomainProvider().getDomain().toArray());
127:
128: final SComboBox cb = new SComboBox(model);
129: cb.setRenderer(new SDefaultListCellRenderer() {
130: public SComponent getListCellRendererComponent(
131: SComponent list, Object value, boolean selected,
132: int row) {
133: SComponent rendererComponent = super
134: .getListCellRendererComponent(list, value,
135: selected, row);
136: setText(chooserAction.getFormat().format(value));
137: return rendererComponent;
138: }
139: });
140: cb.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: chooserAction.actionPerformed(e);
143: model.removeAllElements();
144: List domain = chooserAction.getDomainProvider()
145: .getDomain();
146: for (Object o : domain) {
147: model.addElement(o);
148: }
149: model.setSelectedItem(chooserAction.getSelectedItem());
150: }
151: });
152: Object chooserItem = chooserAction.getSelectedItem();
153: if (chooserItem != null) {
154: model.setSelectedItem(chooserItem);
155: }
156: return cb;
157: }
158:
159: protected SIcon[] createActionBarButtonIcons(String[] txt) {
160: SVGButtonRenderer renderer = SVGButtonRenderer.getInstance();
161: Dimension dim = new Dimension(-1, -1);
162: for (int i = 0; i < txt.length; i++) {
163: if (txt[i] == null || txt[i].length() == 0) {
164: continue;
165: }
166: Dimension newDim = renderer
167: .calculateButtonImageSize(txt[i]);
168: if (newDim.width > dim.width) {
169: dim.width = newDim.width;
170: }
171: if (newDim.height > dim.height) {
172: dim.height = newDim.height;
173: }
174: }
175: // use a fixed width for the buttons
176: dim.width = 150; //dim.width + 10;
177: SIcon[] icons = new SImageIcon[txt.length * 2];
178: for (int i = 0; i < txt.length; i++) {
179: if (txt[i] == null) {
180: continue;
181: }
182: icons[2 * i] = renderer.retrieveRenderedButtonIcon(txt[i],
183: "svg/toolbar_button.svg", dim);
184: icons[2 * i + 1] = renderer.retrieveRenderedButtonIcon(
185: txt[i], "svg/toolbar_button_disabled.svg", dim);
186: }
187: return icons;
188: }
189:
190: public void showDetailHelp(String key) {
191: for (Iterator<Tool> iterator = getTools().iterator(); iterator
192: .hasNext();) {
193: Tool tool = iterator.next();
194: if (key.equals(tool.getValue(Tool.KEY)))
195: iterator.remove();
196: }
197:
198: ContextHelpTool helpTool = new ContextHelpTool();
199: helpTool.setHelpCode(key);
200: helpTool.putValue(Tool.KEY, key);
201: String tabTitle = Client.getInstance().getResourceProvider()
202: .getMessage(key);
203: helpTool.putValue(Tool.NAME, tabTitle);
204: addTool(helpTool);
205: }
206:
207: public void show(SComponent component) {
208: setContentPane(component);
209: refreshTitle();
210: refreshActions();
211: refreshTools();
212: }
213:
214: public void hide(SComponent component) {
215: setContentPane(new SLabel());
216: setTitleCode(null);
217: setControls(null);
218: setTools(null);
219: }
220:
221: protected class SToolBarButton extends SButton {
222:
223: private String text;
224:
225: private SIcon icon;
226:
227: private SIcon disabledIcon;
228:
229: public SToolBarButton(Action action) {
230: super (action);
231: }
232:
233: public String getText() {
234: return text;
235: }
236:
237: public void setText(String text) {
238: this .text = text;
239: }
240:
241: public SIcon getIcon() {
242: return icon;
243: }
244:
245: public void setIcon(SIcon icon) {
246: this .icon = icon;
247: }
248:
249: public void setDisabledIcon(SIcon icon) {
250: this .disabledIcon = icon;
251: }
252:
253: public SIcon getDisabledIcon() {
254: return disabledIcon;
255: }
256: }
257: }
|