001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper.util;
021:
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.datatransfer.DataFlavor;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.util.Iterator;
028: import java.util.List;
029: import javax.swing.AbstractButton;
030: import javax.swing.JLabel;
031:
032: import javax.swing.JSplitPane;
033:
034: import org.netbeans.modules.soa.mapper.common.IMapperEvent;
035: import org.netbeans.modules.soa.mapper.common.IMapperLink;
036: import org.netbeans.modules.soa.mapper.common.IMapperNode;
037: import org.openide.awt.Actions;
038: import org.openide.awt.Mnemonics;
039:
040: /**
041: * <p>
042: *
043: * Title: MapperUtilities </p> <p>
044: *
045: * Description: Provide common functions for all the mapper classes. </p> <p>
046: *
047: * @author Un Seng Leong
048: * @created December 4, 2002
049: */
050: public class MapperUtilities {
051: /**
052: * The jvm local object data flavor.
053: */
054: private static DataFlavor mLocalObjectDataFlavors[] = new DataFlavor[1];
055:
056: static {
057: try {
058: mLocalObjectDataFlavors[0] = new DataFlavor(
059: DataFlavor.javaJVMLocalObjectMimeType);
060: } catch (ClassNotFoundException e) {
061: System.err
062: .println("This should not happen! "
063: + "Can't found DataFlavor.javaJVMLocalObjectMimeType.");
064: e.printStackTrace(System.err);
065: }
066: }
067:
068: /**
069: * No class should instaniate this class.
070: */
071: private MapperUtilities() {
072: }
073:
074: /**
075: * Return the JVM local object data flavor. The data flavor is primary for
076: * default drag and drop data flavor of the mapper. This data flavor is
077: * loaded at the time class is loaded. It is null if
078: * DataFlavor.javaJVMLocalObjectMineType cannot be found during creation of
079: * this data flavor.
080: *
081: * @return the JVM local object data flavor
082: */
083: public static DataFlavor getJVMLocalObjectDataFlavor() {
084: return mLocalObjectDataFlavors[0];
085: }
086:
087: /**
088: * Return the JGoSelection DataFlavor for drag and drop JGoObject operations.
089: * JGo uses JGoSelection as its default transfable object in dnd.
090: *
091: * @return the JGoSelection DataFlavor for drag and drop JGoObject operations.
092: */
093: public static DataFlavor getJGoSelectionDataFlavor() {
094: return com.nwoods.jgo.JGoDocument.getStandardDataFlavor();
095: }
096:
097: /**
098: * Return a new mapper event by specifying the properties of the event.
099: *
100: * @param source the source of the event.
101: * @param transferData the data object to be transfer.
102: * @param eventType the type of the event.
103: * @param eventDesc the description of the event.
104: * @return a new mapper event by specifying the properties of
105: * the event.
106: */
107: public static IMapperEvent getMapperEvent(final Object source,
108: final Object transferData, final String eventType,
109: final String eventDesc) {
110: return new IMapperEvent() {
111:
112: public String getDesc() {
113: return eventDesc;
114: }
115:
116: public String getEventType() {
117: return eventType;
118: }
119:
120: public Object getSource() {
121: return source;
122: }
123:
124: public Object getTransferObject() {
125: return transferData;
126: }
127:
128: public String toString() {
129: return eventDesc;
130: }
131: };
132: }
133:
134: /**
135: * Add a divider location property listener to explicitly set the divider
136: * location with the specified proportion, for the specified JSplitPane, as
137: * the first time the divider location moves.
138: *
139: * @param spliter the JSplitPane to be set.
140: * @param proportion the proportion to change the divider location
141: */
142: public static void addDividerLocationInitializer(
143: JSplitPane spliter, double proportion) {
144: new DividerLocationListener(spliter, proportion);
145: }
146:
147: /**
148: * A generic method to fire a property change event for the specified
149: * listerns.
150: *
151: * @param listeners the listeners that registers
152: * @param source the source of the property change event
153: * @param propertyName the property name that change of the property change
154: * event
155: * @param newValue the new value object of the property change event
156: * @param oldValue the old value object of the property change event
157: */
158: public static void firePropertyChanged(
159: PropertyChangeListener listeners[], Object source,
160: String propertyName, Object newValue, Object oldValue) {
161: if ((listeners == null) || (listeners.length == 0)) {
162: return;
163: }
164:
165: PropertyChangeEvent event = new PropertyChangeEvent(source,
166: propertyName, oldValue, newValue);
167:
168: int i = listeners.length - 1;
169:
170: for (; i >= 0; i--) {
171: listeners[i].propertyChange(event);
172: }
173: }
174:
175: /**
176: * This class listens on the frist divider location change and explicitly
177: * set the divider location with the specified proportion, for a specified
178: * JSplitPane.
179: *
180: * @author sleong
181: * @created December 4, 2002
182: */
183: public static class DividerLocationListener implements
184: PropertyChangeListener {
185: /**
186: * DOCUMENT ME!
187: */
188: private double mProportion = 0.5d;
189:
190: /**
191: * DOCUMENT ME!
192: */
193: private JSplitPane mSpliter;
194:
195: /**
196: * Constructor a DividerLocationListener with a specified JSplitPane to
197: * listen on and a specified proportion to change the divider location.
198: * If the proportion is not between 0.0 and 1.0, an
199: * IllegalArgumentException will be threw.
200: *
201: * @param spliter the JSplitPane this listener listens on.
202: * @param proportion the proportion to change the divider location.
203: */
204: public DividerLocationListener(JSplitPane spliter,
205: double proportion) {
206: if ((proportion < 0.0d) || (proportion > 1.0d)) {
207: throw new java.lang.IllegalArgumentException(
208: "proportion is not between 0.0 and 1.0: "
209: + proportion);
210: }
211:
212: mProportion = proportion;
213: mSpliter = spliter;
214:
215: mSpliter.addPropertyChangeListener(
216: JSplitPane.DIVIDER_LOCATION_PROPERTY, this );
217: }
218:
219: /**
220: * Invoke when the divider location change on the specified JSplitPane,
221: * to explicitly set the divider location to a specified proportion.
222: * This method first remove itself from the JSplitPane. It calculates
223: * the divider location base on the specified proportion and the split
224: * style of the JSplitPane (Horizontal or Vertical).
225: *
226: * @param e the PropertyChangeEvent event
227: */
228: public void propertyChange(PropertyChangeEvent e) {
229: mSpliter.removePropertyChangeListener(
230: JSplitPane.DIVIDER_LOCATION_PROPERTY, this );
231:
232: int length = 0;
233:
234: if (mSpliter.getOrientation() == JSplitPane.VERTICAL_SPLIT) {
235: length = mSpliter.getHeight();
236: } else {
237: length = mSpliter.getWidth();
238: }
239:
240: mSpliter.setDividerLocation((int) (length * mProportion));
241: }
242: }
243:
244: public static boolean isLinkAlreadyConnected(IMapperLink link,
245: IMapperNode node) {
246: // Determines if the link is fully connected (i.e. both start and end
247: // nodes are set). This means that the link is already connected for any
248: // party interested in knowing such a thing.
249: List links = node.getLinks();
250: if (link.getStartNode() != null) {
251: for (Iterator iter = links.iterator(); iter.hasNext();) {
252: IMapperLink iterLink = (IMapperLink) iter.next();
253: if (iterLink.getEndNode() != null) {
254: return true;
255: }
256: }
257: }
258: if (link.getEndNode() != null) {
259: for (Iterator iter = links.iterator(); iter.hasNext();) {
260: IMapperLink iterLink = (IMapperLink) iter.next();
261: if (iterLink.getStartNode() != null) {
262: return true;
263: }
264: }
265: }
266: return false;
267: }
268:
269: /** Activates inline mnemonics defined in a container and its children.
270: * @param owner Parent owner container to use.
271: * @since 5.5
272: */
273: public static void activateInlineMnemonics(Container owner) {
274: Component[] comps = owner.getComponents();
275: for (int i = 0; ((comps != null) && (i < comps.length)); i++) {
276: Component comp = comps[i];
277: if ((comp instanceof JLabel)
278: || (comp instanceof AbstractButton)) {
279: activateInlineMnemonics(comp);
280: } else if (comp instanceof Container) {
281: activateInlineMnemonics((Container) comp);
282: }
283: }
284: }
285:
286: /** Activates inline mnemonics defined in a component.
287: * @param comp Component to use.
288: * @since 5.5
289: */
290: public static void activateInlineMnemonics(Component comp) {
291: if (comp instanceof JLabel) {
292: JLabel label = (JLabel) comp;
293: Mnemonics.setLocalizedText(label, label.getText());
294: } else if (comp instanceof AbstractButton) {
295: AbstractButton button = (AbstractButton) comp;
296: Mnemonics.setLocalizedText(button, button.getText());
297: }
298: }
299:
300: /** Tests if a string is empty.
301: * @param str String to test.
302: * @return <code>true</code> if so.
303: * @since 5.5
304: */
305: public static boolean isEmpty(String str) {
306: return ((null == str) || (str.trim().length() == 0));
307: }
308:
309: /** @see org.openide.awt.Actions#cutAmpersand(java.lang.String)
310: */
311: public static String cutAmpersand(String string) {
312: return Actions.cutAmpersand(string);
313: }
314: }
|