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:
042: /*
043: * NameSpacePanel.java
044: *
045: * Created on June 28, 2006, 4:12 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.schema.abe;
052:
053: import java.awt.BorderLayout;
054: import java.awt.Color;
055: import java.awt.Font;
056: import java.awt.dnd.DropTargetDragEvent;
057: import java.awt.dnd.DropTargetDropEvent;
058: import java.awt.dnd.DropTargetEvent;
059: import java.awt.event.ActionEvent;
060: import java.awt.event.ActionListener;
061: import java.awt.event.KeyAdapter;
062: import java.awt.event.KeyEvent;
063: import java.awt.event.MouseAdapter;
064: import java.awt.event.MouseEvent;
065: import java.beans.PropertyChangeEvent;
066: import java.beans.PropertyChangeListener;
067: import java.net.URI;
068: import java.net.URISyntaxException;
069: import javax.swing.Box;
070: import javax.swing.JPanel;
071: import javax.swing.JSeparator;
072: import javax.swing.JTextField;
073: import javax.swing.border.EmptyBorder;
074: import javax.swing.border.LineBorder;
075: import org.netbeans.modules.xml.axi.AXIComponent;
076: import org.netbeans.modules.xml.axi.AXIDocument;
077: import org.netbeans.modules.xml.axi.AXIModel;
078: import org.netbeans.modules.xml.axi.ContentModel;
079: import org.netbeans.modules.xml.axi.Element;
080: import org.netbeans.modules.xml.schema.abe.nodes.ABEAbstractNode;
081: import org.netbeans.modules.xml.schema.abe.nodes.NamespaceNode;
082: import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
083: import org.openide.util.NbBundle;
084:
085: /**
086: *
087: * @author girix
088: */
089: public class NamespacePanel extends GradientShadePanel {
090: protected static final long serialVersionUID = 7526472295622776147L;
091: private InplaceEditableLabel namespaceLabel;
092:
093: /** Creates a new instance of NameSpacePanel */
094: public NamespacePanel(InstanceUIContext context) {
095: super (context);
096: super .selectedTopGradientColor = Color.WHITE;
097: super .selectedBottomGradientColor = Color.WHITE;
098: initialize();
099: initKeyListener();
100: this .setFocusable(true);
101: this .requestFocusInWindow();
102:
103: }
104:
105: private void initKeyListener() {
106: addKeyListener(new KeyAdapter() {
107: public void keyPressed(KeyEvent e) {
108: if (context.getFocusTraversalManager()
109: .isFocusChangeEvent(e))
110: context.getFocusTraversalManager().handleEvent(e,
111: NamespacePanel.this );
112: }
113:
114: });
115: }
116:
117: private void initialize() {
118: setLayout(new BorderLayout());
119: setOpaque(false);
120: setBorder(new EmptyBorder(1, 1, 1, 1));
121: //setBorder(new javax.swing.border.LineBorder(java.awt.Color.lightGray, 1, true));
122:
123: String namespace = getNamespace();
124: namespaceLabel = new InplaceEditableLabel(namespace);
125: //Font font = namespaceLabel.getFont();
126: //namespaceLabel.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
127: namespaceLabel
128: .setForeground(InstanceDesignConstants.NAMESPACE_COLOR);
129: namespaceLabel.setWidthMagnificationFactor(1);
130: initNamespaceEditListener();
131: initNamespaceChangeListener();
132: add(
133: Box
134: .createHorizontalStrut(TitleWrapperPanel.TITLE_BEGIN_FUDGE),
135: BorderLayout.WEST);
136: add(Box.createVerticalStrut(5), BorderLayout.NORTH);
137: add(namespaceLabel, BorderLayout.CENTER);
138: JPanel botP = new JPanel(new BorderLayout());
139: botP.setOpaque(false);
140: botP.add(Box.createVerticalStrut(5), BorderLayout.NORTH);
141: JSeparator hline = new JSeparator();
142: hline.setForeground(new Color(250, 232, 213));
143: botP.add(hline, BorderLayout.CENTER);
144: add(botP, BorderLayout.SOUTH);
145:
146: /*JLabel msgLabel = new JLabel(" "+
147: NbBundle.getMessage(InstanceDesignerPanel.class, "MSG_DROP_HERE_FOR_ELEMENT"));
148: msgLabel.setForeground(Color.gray);
149: namespacePanel.add(msgLabel, BorderLayout.CENTER);*/
150: //namespacePanel.add(Box.createVerticalStrut(10), BorderLayout.SOUTH);
151: addPropertyChangeListener(new PropertyChangeListener() {
152: public void propertyChange(PropertyChangeEvent evt) {
153: if (evt.getPropertyName().equals(
154: ABEBaseDropPanel.PROP_SELECTED)) {
155: Font font = namespaceLabel.getFont();
156: if (((Boolean) evt.getNewValue()).booleanValue()) {
157: setBorder(new LineBorder(
158: InstanceDesignConstants.XP_ORANGE, 2));
159: namespaceLabel.setFont(new Font(font.getName(),
160: Font.BOLD, font.getSize()));
161: } else {
162: setBorder(new EmptyBorder(1, 1, 1, 1));
163: namespaceLabel.setFont(new Font(font.getName(),
164: Font.PLAIN, font.getSize()));
165: }
166: }
167: }
168: });
169:
170: addMouseListener(new MouseAdapter() {
171: public void mouseClicked(MouseEvent e) {
172: mouseClickedActionHandler(e, false);
173: }
174:
175: public void mouseEntered(MouseEvent e) {
176: mouseClickedActionHandler(e, true);
177: }
178:
179: public void mouseReleased(MouseEvent e) {
180: mouseClickedActionHandler(e, true);
181: }
182: });
183: }
184:
185: public void mouseClickedActionHandler(MouseEvent e,
186: boolean handelPopupOnly) {
187: if (e.getClickCount() == 1) {
188: if (e.isPopupTrigger()) {
189: context.getMultiComponentActionManager().showPopupMenu(
190: e, NamespacePanel.this );
191: return;
192: }
193: if (handelPopupOnly)
194: return;
195: //the attr is selected
196: if (e.isControlDown())
197: context.getComponentSelectionManager()
198: .addToSelectedComponents(NamespacePanel.this );
199: else
200: context.getComponentSelectionManager()
201: .setSelectedComponent(NamespacePanel.this );
202: }
203: }
204:
205: public String getNamespace() {
206: String namespace = context.getAXIModel().getRoot()
207: .getTargetNamespace();
208: if (namespace == null)
209: return NbBundle.getMessage(NamespacePanel.class,
210: "LBL_NO_NAMESPACE");
211: else
212: return namespace;
213: }
214:
215: public void setNamespaceInModel(String nsStr) {
216: AXIModel model = context.getAXIModel();
217: model.startTransaction();
218: try {
219: model.getRoot().setTargetNamespace(nsStr);
220: } finally {
221: model.endTransaction();
222: }
223: }
224:
225: protected void initNamespaceEditListener() {
226: namespaceLabel
227: .addPropertyChangeListener(new PropertyChangeListener() {
228: public void propertyChange(PropertyChangeEvent evt) {
229: if (evt.getPropertyName().equals(
230: namespaceLabel.PROPERTY_MODE_CHANGE)) {
231: if (evt.getNewValue() == InplaceEditableLabel.Mode.EDIT) {
232: //user selected edit give the editor JComponent
233: //show a text field
234: final JTextField field = new JTextField(
235: getNamespace());
236: field.select(0, field.getText()
237: .length());
238: field
239: .addActionListener(new ActionListener() {
240: public void actionPerformed(
241: ActionEvent e) {
242: String newName = field
243: .getText();
244: try {
245: new URI(newName);
246: } catch (URISyntaxException ex) {
247: org.openide.ErrorManager
248: .getDefault()
249: .notify(ex);
250: return;
251: }
252: setNamespaceInModel(newName);
253: namespaceLabel
254: .setText(getNamespace());
255: }
256: });
257: namespaceLabel
258: .setInlineEditorComponent(field);
259: }
260: }
261: }
262: });
263: }
264:
265: public void addElement() {
266: AXIDocument adoc = context.getAXIModel().getRoot();
267: AXIModel model = context.getAXIModel();
268: model.startTransaction();
269: try {
270: Element nelm = model.getComponentFactory().createElement();
271: String str = UIUtilities.getUniqueName(
272: InstanceDesignConstants.NEW_ELEMENT_NAME, adoc);
273: nelm.setName(str);
274: adoc.addElement(nelm);
275: } finally {
276: context.setUserInducedEventMode(true, this );
277: model.endTransaction();
278: }
279: }
280:
281: public void addComplexType() {
282: AXIDocument adoc = context.getAXIModel().getRoot();
283: AXIModel model = context.getAXIModel();
284: model.startTransaction();
285: try {
286: ContentModel nelm = model.getComponentFactory()
287: .createComplexType();
288: String str = UIUtilities.getUniqueName(
289: InstanceDesignConstants.NEW_COMPLEXTYPE_NAME, adoc);
290: nelm.setName(str);
291: adoc.addContentModel(nelm);
292: } finally {
293: context.setUserInducedEventMode(true, this );
294: model.endTransaction();
295: }
296: }
297:
298: public void drop(DropTargetDropEvent event) {
299: super .drop(event);
300: DnDHelper.PaletteItem pi = DnDHelper
301: .getDraggedPaletteItem(event);
302: if ((pi != pi.ELEMENT) && (pi != pi.COMPLEXTYPE))
303: event.rejectDrop();
304: else {
305: context.setUserInducedEventMode(true, this );
306: try {
307: if (pi == pi.ELEMENT) {
308: addElement();
309: } else if (pi == pi.COMPLEXTYPE) {
310: addComplexType();
311: }
312: } finally {
313: context.setUserInducedEventMode(false);
314: }
315: }
316: }
317:
318: public void dragExit(DropTargetEvent event) {
319: super .dragExit(event);
320: }
321:
322: public void dragOver(DropTargetDragEvent event) {
323: super .dragOver(event);
324: DnDHelper.PaletteItem pi = DnDHelper
325: .getDraggedPaletteItem(event);
326: if ((pi != pi.ELEMENT) && (pi != pi.COMPLEXTYPE))
327: event.rejectDrag();
328: }
329:
330: public void dragEnter(DropTargetDragEvent event) {
331: super .dragEnter(event);
332: DnDHelper.PaletteItem pi = DnDHelper
333: .getDraggedPaletteItem(event);
334: if ((pi != pi.ELEMENT) && (pi != pi.COMPLEXTYPE))
335: event.rejectDrag();
336: }
337:
338: NamespaceNode nsNode;
339:
340: public ABEAbstractNode getNBNode() {
341: if (nsNode == null) {
342: nsNode = new NamespaceNode(context.getAXIModel().getRoot(),
343: context);
344: }
345: return nsNode;
346: }
347:
348: public AXIComponent getAXIComponent() {
349: return context.getAXIModel().getRoot();
350: }
351:
352: private void initNamespaceChangeListener() {
353: context.getAXIModel().getRoot().addPropertyChangeListener(
354: new ModelEventMediator(this , context.getAXIModel()
355: .getRoot()) {
356: public void _propertyChange(PropertyChangeEvent evt) {
357: if (evt.getPropertyName().equals(
358: AXIDocument.PROP_TARGET_NAMESPACE)) {
359: namespaceLabel.setText(getNamespace());
360: }
361: }
362: });
363: }
364:
365: public void accept(UIVisitor visitor) {
366: visitor.visit(this);
367: }
368:
369: }
|