KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > EditorView


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.core.windows.view;
22
23
24
25 import java.awt.*;
26 import java.awt.datatransfer.DataFlavor JavaDoc;
27 import java.awt.dnd.*;
28 import java.util.Arrays JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.*;
32 import javax.swing.border.Border JavaDoc;
33 import org.netbeans.core.windows.*;
34 import org.netbeans.core.windows.view.dnd.*;
35 import org.openide.util.*;
36 import org.openide.windows.*;
37
38
39 /**
40  * Class which represents model of editor area element for GUI hierarchy.
41  *
42  * @author Peter Zavadsky
43  */

44 public class EditorView extends ViewElement {
45
46     private static final boolean IS_GTK = "GTK".equals(UIManager.getLookAndFeel().getID()); //NOI18N
47

48     private ViewElement editorArea;
49     
50     private EditorAreaComponent editorAreaComponent;
51     
52     // XXX PENDING
53
private final WindowDnDManager windowDnDManager;
54     
55     
56     public EditorView(Controller controller, WindowDnDManager windowDnDManager,
57     double resizeWeight, ViewElement editorArea) {
58         super(controller, resizeWeight);
59         
60         this.editorArea = editorArea;
61         this.windowDnDManager = windowDnDManager;
62     }
63     
64     
65     // XXX
66
Rectangle getPureBounds() {
67         Component comp = getEditorAreaComponent();
68         Rectangle bounds = comp.getBounds();
69         Point location = new Point(0, 0);
70         javax.swing.SwingUtilities.convertPointToScreen(location, comp);
71         bounds.setLocation(location);
72         return bounds;
73     }
74     
75     private EditorAreaComponent getEditorAreaComponent() {
76         if(editorAreaComponent == null) {
77             editorAreaComponent = new EditorAreaComponent(this, windowDnDManager);
78         }
79
80         // Workaround for #42640
81
if (EditorView.IS_GTK && !editorAreaComponent.isValid()) {
82             editorAreaComponent.repaint();
83         }
84         return editorAreaComponent;
85     }
86     
87     /** Handles special border policy - scroll pane like border only
88      * if editor area is null.
89      */

90     private void manageBorder (JPanel panel) {
91         if (editorArea != null) {
92             panel.setBorder(null);
93         } else {
94             if (Utilities.isMac()) {
95                //#64701 on macosx the nb.scrollpane.border draws ugly line on top
96
panel.setBorder(BorderFactory.createEmptyBorder());
97             } else {
98                 // special border installed into UI manager by netbeans
99
panel.setBorder((Border JavaDoc)UIManager.get("Nb.ScrollPane.border"));
100         }
101         }
102     }
103     
104     public ViewElement getEditorArea() {
105         return editorArea;
106     }
107     
108     public void setEditorArea(ViewElement editorArea) {
109         this.editorArea = editorArea;
110     }
111     
112     public Component getComponent() {
113 // assureComponentInEditorArea();
114
return getEditorAreaComponent();
115     }
116     
117     public boolean updateAWTHierarchy(Dimension availableSpace) {
118 // System.out.println("EditorView:updateAWTHierarchy=" + availableSpace);
119
boolean result = false;
120         EditorAreaComponent comp = getEditorAreaComponent();
121         Dimension d = (Dimension) comp.getClientProperty ("lastAvailableSpace"); //NOI18N
122
Dimension currDim = comp.getPreferredSize();
123         if (!availableSpace.equals(d) || !availableSpace.equals(currDim)) {
124             //We will only return true if we actually did something
125
comp.setPreferredSize(availableSpace);
126 // comp.setMinimumSize(availableSpace);
127
comp.putClientProperty("lastAvailableSpace", availableSpace); //NOI18N
128
result = true;
129         }
130         assureComponentInEditorArea();
131         if (editorArea != null) {
132             result |= editorArea.updateAWTHierarchy(new Dimension(availableSpace.width - 5, availableSpace.height - 5));
133         }
134         return result;
135     }
136     
137     void assureComponentInEditorArea() {
138         EditorAreaComponent eac = getEditorAreaComponent();
139         if(editorArea == null) {
140             eac.setAreaComponent(null);
141         } else {
142             eac.setAreaComponent(editorArea.getComponent());
143         }
144         manageBorder(eac);
145         
146 // // XXX #36885 When in maximixed and compact mode, we cannot add the components
147
// // into the editor area, it would remove it from the screen.
148
// if(addingAllowed) {
149
// if(this.editorArea != null) {
150
// editorAreaComp.add(this.editorArea.getComponent(), BorderLayout.CENTER);
151
// }
152
//
153
// editorAreaComp.validate();
154
// editorAreaComp.repaint();
155
// }
156
}
157
158     private static DataFlavor JavaDoc URI_LIST_DATA_FLAVOR;
159     static {
160         try {
161             URI_LIST_DATA_FLAVOR = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
162         } catch( ClassNotFoundException JavaDoc cnfE ) {
163             cnfE.printStackTrace();
164         }
165     }
166     
167     private static class EditorAreaComponent extends JPanel
168     implements TopComponentDroppable {
169         
170         private final EditorView editorView;
171         
172         // XXX PENDING
173
private final WindowDnDManager windowDnDManager;
174         
175         private Component areaComponent;
176         
177         
178         public EditorAreaComponent(EditorView editorView, WindowDnDManager windowDnDManager) {
179             this.editorView = editorView;
180             this.windowDnDManager = windowDnDManager;
181             
182             init();
183         }
184
185         
186         private void init() {
187             setLayout(new BorderLayout());
188             // special background for XP style
189
String JavaDoc lfID = UIManager.getLookAndFeel().getID();
190 // if (lfID.equals("Windows")) {
191
// setBackground((Color)UIManager.get("nb_workplace_fill"));
192
// }
193

194             // PENDING Adding image into empty area.
195
String JavaDoc imageSource = Constants.SWITCH_IMAGE_SOURCE; // NOI18N
196
if(imageSource != null) {
197                 Image image = Utilities.loadImage(imageSource);
198                 if(image != null) {
199                     JLabel label = new JLabel(new ImageIcon(image));
200                     label.setMinimumSize(new Dimension(0, 0)); // XXX To be able shrink the area.
201
add(label, BorderLayout.CENTER);
202                 } else {
203                     Logger.getLogger(EditorView.class.getName()).log(Level.WARNING, null,
204                                       new java.lang.NullPointerException JavaDoc("Image not found at " +
205                                                                          imageSource)); // NOI18N
206
}
207             }
208             //listen to files being dragged over the editor area
209
DropTarget dropTarget = new DropTarget( this, new DropTargetListener() {
210                 public void dragEnter(DropTargetDragEvent dtde) {
211                 }
212                 public void dragExit(DropTargetEvent dte) {
213                 }
214                 public void dragOver(DropTargetDragEvent dtde) {
215                     ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class );
216                     //check if a file is being dragged over and if anybody can process it
217
if( null != handler && handler.canDrop( dtde ) ) {
218                         dtde.acceptDrag( DnDConstants.ACTION_COPY );
219                     } else {
220                         dtde.rejectDrag();
221                     }
222                 }
223                 public void drop(DropTargetDropEvent dtde) {
224                     ExternalDropHandler handler = (ExternalDropHandler)Lookup.getDefault().lookup( ExternalDropHandler.class );
225                     if( handler.canDrop( dtde ) ) {
226                         //file is being dragged over
227
dtde.acceptDrop( DnDConstants.ACTION_COPY );
228                         //let the handler to take care of it
229
dtde.dropComplete( handler.handleDrop( dtde ) );
230                     } else {
231                         dtde.dropComplete( false );
232                     }
233                 }
234                 public void dropActionChanged(DropTargetDragEvent dtde) {
235                 }
236             } );
237             setDropTarget( dropTarget );
238         }
239         
240         public void setAreaComponent(Component areaComponent) {
241             if(this.areaComponent == areaComponent) {
242                 // XXX PENDING revise how to better manipulate with components
243
// so there don't happen unneeded removals.
244
if(areaComponent != null
245                 && !Arrays.asList(getComponents()).contains(areaComponent)) {
246                     add(areaComponent, BorderLayout.CENTER);
247                 }
248                 
249                 return;
250             }
251             
252             if(this.areaComponent != null) {
253                 remove(this.areaComponent);
254             }
255             
256             this.areaComponent = areaComponent;
257             
258             if(this.areaComponent != null) {
259                 add(this.areaComponent, BorderLayout.CENTER);
260             }
261         }
262         
263         public Shape getIndicationForLocation(Point location) {
264             ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(windowDnDManager.getStartingTransfer());
265             int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR;
266                         
267             if(kind == Constants.MODE_KIND_EDITOR) {
268                 Rectangle rect = getBounds();
269                 rect.setLocation(0, 0);
270                 return rect;
271             } else {
272                 Rectangle rect = getBounds();
273                 rect.setLocation(0, 0);
274
275                 String JavaDoc side = getSideForLocation(location);
276
277                 double ratio = Constants.DROP_AROUND_RATIO;
278                 if(Constants.TOP.equals(side)) {
279                     return new Rectangle(0, 0, rect.width, (int)(rect.height * ratio));
280                 } else if(side == Constants.LEFT) {
281                     return new Rectangle(0, 0, (int)(rect.width * ratio), rect.height);
282                 } else if(side == Constants.RIGHT) {
283                     return new Rectangle(rect.width - (int)(rect.width * ratio), 0, (int)(rect.width * ratio), rect.height);
284                 } else if(side == Constants.BOTTOM) {
285                     return new Rectangle(0, rect.height - (int)(rect.height * ratio), rect.width, (int)(rect.height * ratio));
286                 } else if(Constants.SWITCH_MODE_ADD_NO_RESTRICT
287                 || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(windowDnDManager.getStartingTransfer())) {
288                     return rect;
289                 } else {
290                     return null;
291                 }
292             }
293         };
294         
295         public Object JavaDoc getConstraintForLocation(Point location) {
296             ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(windowDnDManager.getStartingTransfer());
297             int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR;
298                         
299             if(kind == Constants.MODE_KIND_EDITOR) {
300                 return null;
301             } else {
302                 return getSideForLocation(location);
303             }
304         }
305         
306         private String JavaDoc getSideForLocation(Point location) {
307             Rectangle bounds = getBounds();
308             bounds.setLocation(0, 0);
309
310             // Size of area which indicates creation of new split.
311
int delta = Constants.DROP_AREA_SIZE;
312
313             Rectangle top = new Rectangle(0, 0, bounds.width, delta);
314             if(top.contains(location)) {
315                 return Constants.TOP;
316             }
317
318             Rectangle left = new Rectangle(0, delta, delta, bounds.height - 2 * delta);
319             if(left.contains(location)) {
320                 return Constants.LEFT;
321             }
322
323             Rectangle right = new Rectangle(bounds.width - delta, delta, delta, bounds.height - 2 * delta);
324             if(right.contains(location)) {
325                 return Constants.RIGHT;
326             }
327
328             Rectangle bottom = new Rectangle(0, bounds.height - delta, bounds.width, delta);
329             if(bottom.contains(location)) {
330                 return Constants.BOTTOM;
331             }
332
333             return null;
334         }
335         
336         public Component getDropComponent() {
337             return this;
338         }
339         
340         public ViewElement getDropViewElement() {
341             return editorView;
342         }
343         
344         public boolean canDrop(TopComponent transfer, Point location) {
345             if(Constants.SWITCH_MODE_ADD_NO_RESTRICT
346             || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(transfer)) {
347                 return true;
348             }
349             
350             ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(transfer);
351             int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR;
352
353             if(kind == Constants.MODE_KIND_EDITOR) {
354                 return true;
355             } else {
356                 if(WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_JOINED
357                 && getSideForLocation(location) != null) {
358                     return true;
359                 } else {
360                     return false;
361                 }
362             }
363         }
364         
365         public boolean supportsKind(int kind, TopComponent tc) {
366             return true;
367         }
368         
369     } // End of EditorAreaComponent class.
370

371     
372 }
373
374
Popular Tags