001: package net.xoetrope.builder.editor;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Component;
005: import java.awt.Dimension;
006: import java.awt.FlowLayout;
007: import java.awt.Insets;
008: import java.awt.event.ActionEvent;
009: import java.awt.event.ActionListener;
010: import javax.swing.AbstractButton;
011: import javax.swing.BorderFactory;
012: import javax.swing.ImageIcon;
013: import javax.swing.JButton;
014: import javax.swing.JPanel;
015: import javax.swing.JTabbedPane;
016: import javax.swing.JToggleButton;
017: import javax.swing.border.EmptyBorder;
018:
019: import net.xoetrope.builder.editor.events.CommandListener;
020: import net.xoetrope.builder.editor.events.ToolbarListener;
021: import net.xoetrope.xui.XPage;
022:
023: /**
024: * A toolbar class
025: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
026: * $Revision: 1.20 $
027: */
028: public class XToolPalette extends JPanel implements ActionListener {
029: private JToggleButton labelTool, editTool, buttonTool, radioTool,
030: checkTool, metaTool, tableTool, comboTool, hotspotTool,
031: panelTool, textAreaTool, menuTool, scrollPaneTool,
032: scrollableMetaTool, passwordTool, imageMapTool;
033: private AbstractButton currentButton;
034: private JButton cutTool, copyTool, pasteTool;
035: private JButton alignLeft, alignTop, alignRight, alignBottom,
036: alignCenter, justifyHorz, justifyVert;
037: private ToolbarListener toolbarListener;
038:
039: private CommandListener commandListener;
040: private Insets zeroInsets;
041:
042: protected JTabbedPane tabbar;
043: protected JPanel toolPanel, standardPanel;
044:
045: public XToolPalette(String name) {
046: setLayout(new BorderLayout());
047: tabbar = new JTabbedPane();
048: tabbar.setBorder(new EmptyBorder(0, 0, 0, 0));
049: tabbar.setFont(XuiDefaults.defaultFont);
050: toolPanel = new JPanel();
051: toolPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
052: standardPanel = new JPanel();
053: standardPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
054: tabbar.add(toolPanel, "Tools");
055: tabbar.add(standardPanel, "Standard");
056: add(tabbar, BorderLayout.CENTER);
057:
058: setVisible(false);
059: zeroInsets = new Insets(0, 0, 0, 0);
060: // setBorder( BorderFactory.createEtchedBorder() );
061:
062: FlowLayout layout = new FlowLayout();
063: layout.setAlignment(FlowLayout.LEFT);
064: layout.setHgap(2);
065: toolPanel.setLayout(layout);
066:
067: layout = new FlowLayout();
068: layout.setAlignment(FlowLayout.LEFT);
069: layout.setHgap(2);
070: standardPanel.setLayout(layout);
071:
072: addTool(toolPanel, "newicon.gif", -1, XEditorMenu.MENU_NEW_PAGE);
073: addTool(toolPanel, "saveicon.gif", -2,
074: XEditorMenu.MENU_SAVE_PROJECT);
075: addTool(toolPanel, "exiticon.gif", -3,
076: XEditorMenu.MENU_EXIT_IDE);
077: addSpacer(toolPanel);
078:
079: cutTool = addTool(toolPanel, "cuticon.gif", -4,
080: XEditorMenu.MENU_CUT);
081: copyTool = addTool(toolPanel, "copyicon.gif", -5,
082: XEditorMenu.MENU_COPY);
083: pasteTool = addTool(toolPanel, "pasteicon.gif", -6,
084: XEditorMenu.MENU_PASTE);
085: addSpacer(toolPanel);
086:
087: alignLeft = addTool(toolPanel, "alignleft.gif",
088: XEditorMenu.ID_MENU_ALIGN_LEFT, "Align left");
089: alignRight = addTool(toolPanel, "alignright.gif",
090: XEditorMenu.ID_MENU_ALIGN_RIGHT, "Align right");
091: alignTop = addTool(toolPanel, "aligntop.gif",
092: XEditorMenu.ID_MENU_ALIGN_TOP, "Align top");
093: alignBottom = addTool(toolPanel, "alignbottom.gif",
094: XEditorMenu.ID_MENU_ALIGN_BOTTOM, "Align bottom");
095: alignCenter = addTool(toolPanel, "aligncenter.gif",
096: XEditorMenu.ID_MENU_ALIGN_CENTER, "Align center");
097: justifyHorz = addTool(toolPanel, "justifyhorizontal.gif",
098: XEditorMenu.ID_MENU_JUSTIFY_HORZ, "Justify horizontal");
099: justifyVert = addTool(toolPanel, "justifyvertical.gif",
100: XEditorMenu.ID_MENU_JUSTIFY_VERT, "Justify vertical");
101:
102: labelTool = addToggleTool(standardPanel, "labelicon.gif",
103: XPage.LABEL);
104: editTool = addToggleTool(standardPanel, "editicon.gif",
105: XPage.EDIT);
106: textAreaTool = addToggleTool(standardPanel, "textareaicon.gif",
107: XPage.TEXTAREA);
108: passwordTool = addToggleTool(standardPanel, "passwordicon.gif",
109: "Password");
110: buttonTool = addToggleTool(standardPanel, "buttonicon.gif",
111: XPage.BUTTON);
112: radioTool = addToggleTool(standardPanel, "radioicon.gif",
113: XPage.RADIO);
114: checkTool = addToggleTool(standardPanel, "checkicon.gif",
115: XPage.CHECK);
116: metaTool = addToggleTool(standardPanel, "metaicon.gif",
117: XPage.METACONTENT);
118: scrollableMetaTool = addToggleTool(standardPanel,
119: "smetaicon.gif", XPage.SCROLLABLEMETACONTENT);
120: tableTool = addToggleTool(standardPanel, "tableicon.gif",
121: XPage.TABLE);
122: comboTool = addToggleTool(standardPanel, "comboicon.gif",
123: XPage.COMBO);
124: hotspotTool = addToggleTool(standardPanel, "hotspoticon.gif",
125: XPage.HOTSPOTIMAGE);
126: imageMapTool = addToggleTool(standardPanel, "imagemapicon.gif",
127: "ImageMap");
128: panelTool = addToggleTool(standardPanel, "panelicon.gif",
129: XPage.PANEL);
130: scrollPaneTool = addToggleTool(standardPanel,
131: "scrollpaneicon.gif", XPage.SCROLLPANE);
132: menuTool = addToggleTool(standardPanel, "menuicon.gif",
133: XPage.MENU);
134:
135: initEditTools(false, false, false);
136: initAlignmentTools(false);
137:
138: doLayout();
139: }
140:
141: /**
142: * Add an toggle button to the toolbar.
143: * @param targetPanel the tab panel to which the new tool is added
144: * @param imageName the name of the image resource
145: * @param compId the ID of the component type
146: * @param tooltipText the text of the tooltip
147: * @return the new image component
148: */
149: public JToggleButton addToggleTool(JPanel targetPanel,
150: String imageName, String tooltipText) {
151: JToggleButton tool = new JToggleButton();
152: ImageIcon icon = new ImageIcon(XEditorResourceManager
153: .getImage(imageName));
154: tool.setIcon(icon);
155: tool.setSize(new Dimension(32, 32));
156: targetPanel.add(tool);
157:
158: tool.addActionListener(this );
159: tool.setName(tooltipText);
160: tool.setMargin(zeroInsets);
161: tool.setToolTipText(tooltipText);
162: return tool;
163: }
164:
165: /**
166: * Add an toggle button to the toolbar.
167: * @param targetPanel the tab panel to which the new tool is added
168: * @param imageName the name of the image resource
169: * @param compId the ID of the component type
170: * @param tooltipText the text of the tooltip
171: * @return the new image component
172: */
173: public JButton addTool(JPanel targetPanel, String imageName,
174: int compId, String tooltipText) {
175: JButton tool = new JButton();
176: ImageIcon icon = new ImageIcon(XEditorResourceManager
177: .getImage(imageName));
178: tool.setIcon(icon);
179: tool.setSize(new Dimension(32, 32));
180: targetPanel.add(tool);
181:
182: tool.addActionListener(this );
183: tool.setName(String.valueOf(compId));
184: tool.setMargin(zeroInsets);
185: tool.setToolTipText(tooltipText);
186: return tool;
187: }
188:
189: public void addSpacer(JPanel targetPanel) {
190: JToggleButton tool = new JToggleButton();
191: tool.setSize(new Dimension(6, 32));
192: tool.setBorder(new EmptyBorder(2, 2, 2, 2));
193: targetPanel.add(tool);
194: tool.setMargin(zeroInsets);
195: }
196:
197: /**
198: * Reset the toolpalette to show the current tool selection
199: * @param btn
200: */
201: public void resetButtons() {
202: setSelected(labelTool);
203: setSelected(editTool);
204: setSelected(buttonTool);
205: setSelected(radioTool);
206: setSelected(checkTool);
207: setSelected(metaTool);
208: setSelected(tableTool);
209: // setSelected( comboTool );
210: setSelected(hotspotTool);
211: }
212:
213: /**
214: * Change the state of the edit menu
215: * @param cut enable the cut menu item
216: * @param copy enable the copy menu item
217: * @param paste enable the paste menu item
218: */
219: public void initEditTools(boolean cut, boolean copy, boolean paste) {
220: cutTool.setEnabled(cut);
221: copyTool.setEnabled(copy);
222: pasteTool.setEnabled(paste);
223: }
224:
225: /**
226: * Initialize the alignment toggle buttons
227: * @param enabled true to enable the buttons
228: */
229: public void initAlignmentTools(boolean enabled) {
230: alignLeft.setEnabled(enabled);
231: alignTop.setEnabled(enabled);
232: alignRight.setEnabled(enabled);
233: alignBottom.setEnabled(enabled);
234: alignCenter.setEnabled(enabled);
235: justifyHorz.setEnabled(enabled);
236: justifyVert.setEnabled(enabled);
237: }
238:
239: /**
240: * Add an array of components to the toolbar
241: * @param tabName the name of the toolbar tab associated with these components
242: * @param components the components
243: */
244: public void insertComponents(String tabName, Component[] components) {
245: if (tabName != null) {
246: JPanel newTab = new JPanel();
247: newTab.setBorder(new EmptyBorder(0, 0, 0, 0));
248: FlowLayout layout = new FlowLayout();
249: layout.setAlignment(FlowLayout.LEFT);
250: layout.setHgap(2);
251: newTab.setLayout(layout);
252:
253: int numComps = components.length;
254: for (int i = 0; i < numComps; i++) {
255: newTab.add(components[i]);
256: if (components[i] instanceof JButton)
257: ((JButton) components[i]).addActionListener(this );
258: }
259: tabbar.add(newTab, tabName);
260: }
261: }
262:
263: /**
264: * Remove the tab with the set of named components
265: * @param tabName the tab name
266: */
267: public void removeComponents(String tabName) {
268: if (tabName != null) {
269: int numComps = tabbar.getComponentCount();
270: for (int i = 0; i < numComps; i++) {
271: Component comp = getComponent(i);
272: if (comp.getName().equals(tabName)) {
273: tabbar.remove(comp);
274: break;
275: }
276: }
277: tabbar.repaint();
278: }
279: }
280:
281: private void setSelected(JToggleButton current) {
282: current.setSelected(false);
283: }
284:
285: public void setToolbarListener(ToolbarListener listener) {
286: toolbarListener = listener;
287: }
288:
289: public void actionPerformed(ActionEvent evt) {
290: AbstractButton btn = (AbstractButton) evt.getSource();
291: if (!btn.equals(currentButton) && (currentButton != null)
292: && (currentButton instanceof JToggleButton))
293: ((JToggleButton) currentButton).setSelected(false);
294:
295: currentButton = btn;
296: String cmd = btn.getName();
297: int id = 0;
298: try {
299: id = Integer.parseInt(cmd);
300: } catch (NumberFormatException ex) {
301: }
302:
303: if (id < 0) {
304: commandListener.processCommand(XEditorMenu.getCommandId(btn
305: .getToolTipText()), null, false);
306: if (currentButton instanceof JToggleButton)
307: ((JToggleButton) currentButton).setSelected(false);
308: } else {
309: if (btn.isSelected())
310: toolbarListener.toolbarClicked(cmd);
311: else
312: toolbarListener.toolbarClicked(null);
313: }
314: }
315:
316: public void setCommandListener(CommandListener listener) {
317: commandListener = listener;
318: }
319:
320: public void componentAdded() {
321: if ((currentButton != null)
322: && (currentButton instanceof JToggleButton))
323: ((JToggleButton) currentButton).setSelected(false);
324: }
325:
326: /**
327: * Set the currently selected component on the toolbar
328: * @param btn selected object
329: */
330: public void setCurrentButton(AbstractButton btn) {
331: if ((currentButton instanceof JToggleButton)
332: && (currentButton != btn))
333: ((JToggleButton) currentButton).setSelected(false);
334: currentButton = btn;
335: }
336: }
|