001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.graph.view.impl;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.awt.Insets;
046: import java.awt.Point;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.ActionListener;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.util.List;
052: import java.util.Vector;
053:
054: import org.netbeans.modules.sql.framework.ui.graph.IGraphController;
055: import org.netbeans.modules.sql.framework.ui.graph.IGraphView;
056: import org.netbeans.modules.sql.framework.ui.graph.IOperatorManager;
057: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
058: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfoCategory;
059: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfoModel;
060: import org.openide.nodes.Children;
061: import org.openide.nodes.Node;
062:
063: /**
064: * Tool bar for use in transformation, data filter and data validation editors.
065: *
066: * @author Ritesh Adval
067: * @author Girish Patil
068: * @author Jonathan Giron
069: * @version $Revision$
070: */
071:
072: public class SQLToolBar extends BasicToolBar {
073:
074: class ToolBarItemActionListener implements ActionListener {
075: /**
076: * Invoked when an action occurs.
077: *
078: * @param e ActionEvent to handle
079: */
080: public void actionPerformed(ActionEvent e) {
081: Object src = e.getSource();
082: SQLToolBarMenuItem toolItem = (SQLToolBarMenuItem) src;
083: IGraphView view = SQLToolBar.this .manager.getGraphView();
084: if (view != null) {
085: IGraphController controller = view.getGraphController();
086: if (controller != null) {
087: controller
088: .handleNodeAdded(toolItem.getItemObject(),
089: new Point(50, 50));
090: }
091: }
092: }
093: }
094:
095: class ToolBarPropertyChangeListener implements
096: PropertyChangeListener {
097: public void propertyChange(PropertyChangeEvent evt) {
098: String propName = evt.getPropertyName();
099: if (IOperatorXmlInfo.OPERATOR_CHECKED.equals(propName)) {
100: Boolean val = (Boolean) evt.getNewValue();
101: IOperatorXmlInfo node = (IOperatorXmlInfo) evt
102: .getSource();
103:
104: SQLToolBarMenuItem item = SQLToolBar.this
105: .findToolBarItem(node);
106: if (item != null) {
107: item.setVisible(val.booleanValue());
108: }
109: } else if (IOperatorXmlInfo.OPERATOR_DROPPED
110: .equals(propName)) {
111: IOperatorXmlInfo node = (IOperatorXmlInfo) evt
112: .getSource();
113: IGraphView view = SQLToolBar.this .manager
114: .getGraphView();
115:
116: if (view != null) {
117: IGraphController controller = view
118: .getGraphController();
119: if (controller != null) {
120: controller.handleNodeAdded(node, new Point(50,
121: 50));
122: }
123: }
124: }
125: }
126: }
127:
128: private ToolBarItemActionListener aListener;
129: private ToolBarPropertyChangeListener pListener;
130: private SQLToolBarMenu mActiveMenu;
131: private List mMenus = new Vector();
132: private IOperatorXmlInfoModel model;
133: private IOperatorManager manager;
134:
135: public SQLToolBar() {
136: pListener = new ToolBarPropertyChangeListener();
137: aListener = new ToolBarItemActionListener();
138:
139: setFloatable(false);
140: setBorder(null);
141: this .setMargin(new Insets(0, 0, 0, 0));
142: }
143:
144: public SQLToolBar(IOperatorManager mgr) {
145: this ();
146: manager = mgr;
147: model = mgr.getOperatorXmlInfoModel();
148: }
149:
150: public SQLToolBarMenu getActiveMenu() {
151: synchronized (getTreeLock()) {
152: return mActiveMenu;
153: }
154: }
155:
156: /**
157: * @see java.swing.JToolBar#getMinimumSize
158: */
159: @Override
160: public Dimension getMinimumSize() {
161: return new Dimension(0, 0);
162: }
163:
164: public SQLToolBarMenu getNextMenu(SQLToolBarMenu menu) {
165: synchronized (getTreeLock()) {
166: for (int i = 0; i < mMenus.size(); i++) {
167: Object comp = mMenus.get(i);
168: if (comp == menu) {
169: SQLToolBarMenu nxtMenu = null;
170: if (i == mMenus.size() - 1) {
171: nxtMenu = (SQLToolBarMenu) mMenus.get(0);
172: } else {
173: nxtMenu = (SQLToolBarMenu) mMenus.get(i + 1);
174: }
175:
176: if (!nxtMenu.isVisible()) {
177: nxtMenu = menu;
178: }
179: return nxtMenu;
180: }
181: }
182: }
183: throw new IllegalArgumentException(
184: "The menu specified is not in this group.");
185: }
186:
187: public SQLToolBarMenu getPreviousMenu(SQLToolBarMenu menu) {
188: synchronized (getTreeLock()) {
189: for (int i = 0; i < mMenus.size(); i++) {
190: Object comp = mMenus.get(i);
191: if (comp == menu) {
192: if (i == 0) {
193: return (SQLToolBarMenu) mMenus.get(mMenus
194: .size() - 1);
195: } else {
196: return (SQLToolBarMenu) mMenus.get(i - 1);
197: }
198: }
199: }
200: }
201: throw new IllegalArgumentException(
202: "The menu specified is not in this group.");
203: }
204:
205: /**
206: * Initializes contents of the toolbar.
207: */
208: @Override
209: public void initializeToolBar() {
210: // Remove all old toolbar components
211: this .removeAll();
212: this .mMenus.clear();
213:
214: // Add any custom components first
215: super .initializeToolBar();
216: }
217:
218: public void initializeSQLToolBar() {
219: // Add SQL operators
220: Node node = model.getRootNode();
221: Children children = node.getChildren();
222: Node[] nodes = children.getNodes();
223:
224: for (int i = 0; i < nodes.length; i++) {
225: IOperatorXmlInfoCategory catNode = (IOperatorXmlInfoCategory) nodes[i];
226: if (shouldDisplay(catNode.getToolbarType())) {
227: createOperatorCategories(catNode);
228: }
229: }
230: }
231:
232: public void setActiveMenu(SQLToolBarMenu menu) {
233: synchronized (getTreeLock()) {
234: if (mActiveMenu != menu) {
235: if (mActiveMenu != null) {
236: // if menu is null the active menu already
237: // closed
238: if (menu != null) {
239: mActiveMenu.hideButtonMenu();
240: }
241: }
242: }
243: mActiveMenu = menu;
244: }
245: }
246:
247: @Override
248: public void setEnabled(boolean enable) {
249: if (this .isEnabled() == enable) {
250: return;
251: }
252: super .setEnabled(enable);
253: synchronized (getTreeLock()) {
254: for (int i = 0; i < getComponentCount(); i++) {
255: Component comp = getComponent(i);
256: comp.setEnabled(enable);
257: }
258: }
259: }
260:
261: private boolean shouldDisplay(int nodeType) {
262: return (IOperatorXmlInfoModel.CATEGORY_ALL == nodeType || ((manager
263: .getToolbarType() & nodeType) != 0));
264: }
265:
266: private void createOperatorCategories(
267: IOperatorXmlInfoCategory catNode) {
268: SQLToolBarMenu menu = new SQLToolBarMenu(catNode, manager, this );
269: mMenus.add(menu);
270:
271: // Now add each one of the operator in this category.
272: Children children = ((Node) catNode).getChildren();
273: Node[] nodes = children.getNodes();
274:
275: for (int i = 0; i < nodes.length; i++) {
276: IOperatorXmlInfo opNode = (IOperatorXmlInfo) nodes[i];
277: if (shouldDisplay(opNode.getToolbarType())) {
278: SQLToolBarMenuItem bvi = menu.addMenuItem(opNode);
279:
280: if (bvi != null) {
281: opNode.addPropertyChangeListener(pListener);
282: bvi.addActionListener(aListener);
283: }
284: }
285: }
286:
287: // Always append a separator before appending the category menu.
288: this .addSeparator(new Dimension(1, 0));
289: this .addSeparator(new Dimension(2, 30));
290: this .addSeparator(new Dimension(1, 0));
291: this .add(menu);
292: }
293:
294: private SQLToolBarMenuItem findToolBarItem(IOperatorXmlInfo node) {
295: SQLToolBarMenuItem bvi = null;
296:
297: for (int i = 0; i < this .getComponentCount(); i++) {
298: Component comp = this .getComponentAtIndex(i);
299:
300: if (comp instanceof SQLToolBarMenu) {
301: SQLToolBarMenu menu = (SQLToolBarMenu) this
302: .getComponentAtIndex(i);
303:
304: bvi = menu.findMenuItem(node);
305:
306: if (bvi != null) {
307: break;
308: }
309: }
310: }
311:
312: return bvi;
313: }
314: }
|