KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > views > LayerView


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 05.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.views;
9
10 import java.util.EventObject JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.gef.EditPart;
17 import org.eclipse.gef.EditPartListener;
18 import org.eclipse.gef.RootEditPart;
19 import org.eclipse.gef.commands.CommandStackListener;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.custom.ScrolledComposite;
24 import org.eclipse.swt.events.FocusAdapter;
25 import org.eclipse.swt.events.FocusEvent;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.SelectionListener;
29 import org.eclipse.swt.graphics.Color;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.FillLayout;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.ISelectionListener;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.part.ViewPart;
40
41 import com.nightlabs.editor2d.AbstractEditor;
42 import com.nightlabs.editor2d.DrawComponent;
43 import com.nightlabs.editor2d.Editor;
44 import com.nightlabs.editor2d.EditorPlugin;
45 import com.nightlabs.editor2d.Layer;
46 import com.nightlabs.editor2d.MultiLayerDrawComponent;
47 import com.nightlabs.editor2d.command.CreateLayerCommand;
48 import com.nightlabs.editor2d.command.DeleteLayerCommand;
49 import com.nightlabs.editor2d.edit.LayerEditPart;
50 import com.nightlabs.editor2d.edit.MultiLayerDrawComponentEditPart;
51
52 public class LayerView
53 extends ViewPart
54 implements ISelectionListener
55 {
56   public static final Logger LOGGER = Logger.getLogger(LayerView.class);
57   
58   public static final String JavaDoc ID_VIEW = LayerView.class.getName();
59   
60   public static final Image EYE_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/eye12.gif").createImage();
61 // public static final Image DELETE_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/delete16.gif").createImage();
62
// public static final Image DELETE_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/minus16.gif").createImage();
63
public static final Image DELETE_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/delete24.gif").createImage();
64 // public static final Image NEW_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/add16.gif").createImage();
65
public static final Image NEW_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/plus16.gif").createImage();
66   public static final Image UP_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/up16.gif").createImage();
67   public static final Image DOWN_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/down16.gif").createImage();
68   public static final Image LOCK_ICON = ImageDescriptor.createFromFile(EditorPlugin.class,"icons/lock12.gif").createImage();
69   
70   protected MultiLayerDrawComponent mldc;
71   protected AbstractEditor editor;
72   protected Map JavaDoc button2Layer = new HashMap JavaDoc();
73   protected Composite layerComposite;
74   protected ScrolledComposite scrollComposite;
75   protected Button buttonUp;
76   protected Button buttonDown;
77   protected Button buttonNew;
78   protected Button buttonDelete;
79   protected Color currentLayerColor;
80       
81   /**
82    *
83    */

84   public LayerView() {
85     super();
86   }
87   
88   /* (non-Javadoc)
89    * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
90    */

91   public void createPartControl(Composite parent)
92   {
93     if (getSite().getPage().getActiveEditor() instanceof AbstractEditor)
94     {
95       LOGGER.debug("getSite().getPage().getActiveEditor() instanceof Editor!");
96       editor = (AbstractEditor) getSite().getPage().getActiveEditor();
97       // instead of listen to the CommandStack
98
// listen to the MultiLayerDrawComponentEditPart
99
// editor.getOutlineEditDomain().getCommandStack().addCommandStackListener(commandStackListener);
100
RootEditPart rootEditPart = editor.getOutlineGraphicalViewer().getRootEditPart();
101       List JavaDoc children = rootEditPart.getChildren();
102       if (!children.isEmpty()) {
103         EditPart editPart = (EditPart) children.get(0);
104         if (editPart instanceof MultiLayerDrawComponentEditPart) {
105           MultiLayerDrawComponentEditPart mldcEditPart = (MultiLayerDrawComponentEditPart) editPart;
106           mldcEditPart.addEditPartListener(mldcListener);
107         }
108       }
109       mldc = editor.getMultiLayerDrawComponent();
110     }
111     
112     // add SelectionChangeListener
113
getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
114     
115     // Create parent layout
116
GridLayout parentLayout = new GridLayout();
117     GridData parentData = new GridData();
118     parent.setLayout(parentLayout);
119             
120     // Create LayerComposite
121
scrollComposite = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
122     scrollComposite.setExpandHorizontal(true);
123     layerComposite = new Composite(scrollComposite, SWT.NONE);
124     layerComposite.setSize(scrollComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
125     scrollComposite.setContent(layerComposite);
126             
127     GridData layersData = new GridData();
128     layersData.grabExcessHorizontalSpace = true;
129     layersData.grabExcessVerticalSpace = true;
130     layersData.horizontalAlignment = SWT.FILL;
131     layersData.verticalAlignment = SWT.FILL;
132     scrollComposite.setLayoutData(layersData);
133     scrollComposite.setMinSize(layerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
134         
135 // FillLayout layerFillLayout = new FillLayout();
136
// layerFillLayout.type = SWT.VERTICAL;
137
// layerComposite.setLayout(layerFillLayout);
138
GridLayout layerGridLayout = new GridLayout();
139     layerComposite.setLayout(layerGridLayout);
140             
141     // Create Entries
142
createTools(parent);
143     refresh();
144     scrollComposite.layout(true);
145   }
146
147   private void addLayer()
148   {
149         // create new Layer
150
CreateLayerCommand addLayer = new CreateLayerCommand(mldc);
151         editor.getOutlineEditDomain().getCommandStack().execute(addLayer);
152   }
153     
154     private void createLayerEntry(Composite parent, Layer l)
155     {
156       // create parentComposite
157
Composite parentComposite = new Composite(parent, SWT.BORDER);
158         GridLayout parentLayout = new GridLayout();
159         parentLayout.numColumns = 3;
160         GridData parentData = new GridData();
161         parentData.grabExcessHorizontalSpace = true;
162         parentData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
163         parentData.grabExcessVerticalSpace = false;
164         parentData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
165         parentData.grabExcessHorizontalSpace = true;
166         parentComposite.setLayout(parentLayout);
167         parentComposite.setLayoutData(parentData);
168         
169         // create Visible-Button
170
Button buttonVisible = new Button(parentComposite, SWT.TOGGLE);
171         buttonVisible.setSelection(!l.isVisible());
172         buttonVisible.setImage(EYE_ICON);
173         buttonVisible.setToolTipText(EditorPlugin.getResourceString("layer_visible"));
174         buttonVisible.addSelectionListener(visibleListener);
175         GridData gridDataV = new GridData();
176         gridDataV.verticalAlignment = GridData.BEGINNING;
177         buttonVisible.setLayoutData(gridDataV);
178         
179         // create Editable-Button
180
Button buttonEditble = new Button(parentComposite, SWT.TOGGLE);
181         buttonEditble.setSelection(!l.isEditable());
182         buttonEditble.setImage(LOCK_ICON);
183         buttonEditble.setToolTipText(EditorPlugin.getResourceString("layer_locked"));
184         buttonEditble.addSelectionListener(editableListener);
185         GridData gridDataE = new GridData();
186         gridDataE.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
187         gridDataE.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
188         buttonEditble.setLayoutData(gridDataE);
189         
190         // create Name-Text
191
Text text = new Text(parentComposite, SWT.PUSH);
192         text.addSelectionListener(textListener);
193         text.addFocusListener(focusListener);
194     text.setEditable(true);
195         
196     // TODO: Name should already be set when the command is executed
197
if (l.getName() == null)
198           text.setText(EditorPlugin.getResourceString("layer_default_name"));
199         else
200           text.setText(l.getName());
201                 
202         GridData gridDataText = new GridData();
203         gridDataText.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
204         gridDataText.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
205         gridDataText.grabExcessHorizontalSpace = true;
206         text.setLayoutData(gridDataText);
207         
208         // add newLayer to button2Layer
209
button2Layer.put(buttonVisible, l);
210         button2Layer.put(buttonEditble, l);
211         button2Layer.put(text, l);
212
213         if (l.equals(mldc.getCurrentLayer()))
214         {
215           if (currentLayerColor == null)
216             currentLayerColor = new Color(parent.getDisplay(), 155, 155, 155);
217           
218           parentComposite.setBackground(currentLayerColor);
219         }
220     }
221   
222     private void createTools(Composite parent)
223     {
224       // create ParentLayout
225
Composite toolsComposite = new Composite(parent, SWT.NONE);
226         GridLayout parentLayout = new GridLayout();
227         parentLayout.numColumns = 4;
228         GridData parentData = new GridData();
229         parentData.horizontalAlignment = GridData.END;
230         toolsComposite.setLayout(parentLayout);
231         toolsComposite.setLayoutData(parentData);
232                         
233         // create Buttons
234
buttonUp = new Button(toolsComposite, SWT.NONE);
235         buttonUp.setText("Up");
236 // buttonUp.setImage(UP_ICON);
237
buttonUp.setToolTipText(EditorPlugin.getResourceString("layer_up"));
238         buttonUp.addSelectionListener(upListener);
239         buttonDown = new Button(toolsComposite, SWT.NONE);
240         buttonDown.setText("Down");
241 // buttonDown.setImage(DOWN_ICON);
242
buttonDown.setToolTipText(EditorPlugin.getResourceString("layer_down"));
243         buttonDown.addSelectionListener(downListener);
244         buttonNew = new Button(toolsComposite, SWT.NONE);
245         buttonNew.setText("New");
246 // buttonNew.setImage(NEW_ICON);
247
buttonNew.setToolTipText(EditorPlugin.getResourceString("layer_new"));
248         buttonNew.addSelectionListener(newListener);
249         buttonDelete = new Button(toolsComposite, SWT.NONE);
250         buttonDelete.setText("Delete");
251 // buttonDelete.setImage(DELETE_ICON);
252
buttonDelete.setToolTipText(EditorPlugin.getResourceString("layer_delete"));
253         buttonDelete.addSelectionListener(deleteListener);
254     }
255     
256   /* (non-Javadoc)
257    * @see org.eclipse.ui.IWorkbenchPart#setFocus()
258    */

259   public void setFocus()
260   {
261     
262   }
263
264   protected FocusAdapter focusListener = new FocusAdapter()
265   {
266     public void focusGained(FocusEvent e)
267     {
268             Text t = (Text) e.getSource();
269             if (button2Layer.containsKey(t))
270             {
271                 Layer currLayer = (Layer) button2Layer.get(t);
272                 if (!mldc.getCurrentLayer().equals(currLayer)) {
273                     mldc.setCurrentLayer(currLayer);
274                     LOGGER.debug("Layer "+currLayer.getName()+" = CurrentLayer");
275                     refresh();
276                 }
277             } else {
278               throw new IllegalStateException JavaDoc("There is no such Layer registered!");
279             }
280     }
281   };
282   
283   protected SelectionListener textListener = new SelectionAdapter()
284     {
285         public void widgetDefaultSelected(SelectionEvent e)
286         {
287       if (e.getSource() instanceof Text)
288       {
289         Text text = (Text) e.getSource();
290         String JavaDoc layerName = text.getText();
291         LOGGER.debug("New LayerName = "+layerName);
292         Layer l = (Layer) button2Layer.get(text);
293         l.setName(layerName);
294       }
295         }
296     };
297     
298     protected SelectionListener visibleListener = new SelectionAdapter()
299     {
300         public void widgetSelected(SelectionEvent e)
301         {
302             LOGGER.debug("VISIBLE widgetSelected()");
303             
304             Button b = (Button) e.getSource();
305             if (button2Layer.containsKey(b)) {
306                 Layer l = (Layer) button2Layer.get(b);
307                 if (!b.getSelection()) {
308                     l.setVisible(true);
309                 } else {
310                     l.setVisible(false);
311                 }
312                 
313                 editor.getEditPartViewer().getRootEditPart().refresh();
314             }
315             else {
316                 throw new IllegalStateException JavaDoc("There is no such Layer registered!");
317             }
318         }
319     };
320
321     protected SelectionListener editableListener = new SelectionAdapter()
322     {
323         public void widgetSelected(SelectionEvent e)
324         {
325           LOGGER.debug("EDITABLE widgetSelected()");
326             
327             Button b = (Button) e.getSource();
328             if (button2Layer.containsKey(b)) {
329                 Layer l = (Layer) button2Layer.get(b);
330                 if (b.getSelection()) {
331                     l.setEditable(true);
332                 } else {
333                     l.setEditable(false);
334                 }
335                 editor.getEditorSite().getShell().update();
336             }
337             else {
338                 throw new IllegalStateException JavaDoc("There is no such Layer registered!");
339             }
340         }
341     };
342         
343     protected SelectionListener newListener = new SelectionAdapter()
344     {
345         public void widgetSelected(SelectionEvent e)
346         {
347           LOGGER.debug("NEW widgetSelected()");
348             addLayer();
349 // refresh();
350
}
351     };
352
353     protected SelectionListener deleteListener = new SelectionAdapter()
354     {
355         public void widgetSelected(SelectionEvent e)
356         {
357           LOGGER.debug("DELETE widgetSelected()");
358             
359           Layer currentLayer = mldc.getCurrentLayer();
360           DeleteLayerCommand layerCommand = new DeleteLayerCommand(mldc, currentLayer);
361           editor.getOutlineEditDomain().getCommandStack().execute(layerCommand);
362           
363 // int index = mldc.getDrawComponents().indexOf(mldc.getCurrentLayer());
364
// mldc.getDrawComponents().remove(index);
365
//
366
// if (index != 0) {
367
// mldc.setCurrentLayer((Layer) mldc.getDrawComponents().get(index-1));
368
// } else if ( index==0 && mldc.getDrawComponents().size() > 2) {
369
// mldc.setCurrentLayer((Layer) mldc.getDrawComponents().get(index+1));
370
// }
371

372 // refresh();
373
}
374     };
375
376     protected SelectionListener upListener = new SelectionAdapter()
377     {
378         public void widgetSelected(SelectionEvent e)
379         {
380           LOGGER.debug("UP widgetSelected()");
381         }
382     };
383     
384     protected SelectionListener downListener = new SelectionAdapter()
385     {
386         public void widgetSelected(SelectionEvent e)
387         {
388           LOGGER.debug("DOWN widgetSelected()");
389         }
390     };
391         
392     protected CommandStackListener commandStackListener = new CommandStackListener()
393     {
394     /* (non-Javadoc)
395      * @see org.eclipse.gef.commands.CommandStackListener#commandStackChanged(java.util.EventObject)
396      */

397     public void commandStackChanged(EventObject JavaDoc event)
398     {
399             editor.getEditPartViewer().getRootEditPart().refresh();
400             refresh();
401     }
402   };
403
404   protected EditPartListener mldcListener = new EditPartListener.Stub()
405   {
406     /* (non-Javadoc)
407      * @see org.eclipse.gef.EditPartListener#childAdded(org.eclipse.gef.EditPart, int)
408      */

409     public void childAdded(EditPart child, int index) {
410       if (child instanceof LayerEditPart) {
411         MultiLayerDrawComponentEditPart parent = (MultiLayerDrawComponentEditPart) child.getParent();
412         mldc = (MultiLayerDrawComponent) parent.getModel();
413         refresh();
414       }
415     }
416
417     /* (non-Javadoc)
418      * @see org.eclipse.gef.EditPartListener#removingChild(org.eclipse.gef.EditPart, int)
419      */

420     public void removingChild(EditPart child, int index) {
421       if (child instanceof LayerEditPart) {
422         MultiLayerDrawComponentEditPart parent = (MultiLayerDrawComponentEditPart) child.getParent();
423         mldc = (MultiLayerDrawComponent) parent.getModel();
424         refresh();
425       }
426     }
427   };
428   
429 // private KeyAdapter textListener = new KeyAdapter()
430
// {
431
// public void keyReleased(KeyEvent e)
432
// {
433
// if (e.getSource() instanceof Text)
434
// {
435
// Text text = (Text) e.getSource();
436
// if (e.keyCode == 13) {
437
// String layerName = text.getText();
438
// LOGGER.debug("New LayerName = "+layerName);
439
// Layer l = (Layer) button2Layer.get(text);
440
// l.setName(layerName);
441
// }
442
// }
443
// }
444
// };
445

446     private void deactivateTools(boolean newButton)
447     {
448       if (!buttonUp.isDisposed())
449         buttonUp.setEnabled(false);
450       if (!buttonDown.isDisposed())
451         buttonDown.setEnabled(false);
452       if (!buttonDelete.isDisposed())
453         buttonDelete.setEnabled(false);
454       if (!buttonNew.isDisposed())
455         buttonNew.setEnabled(newButton);
456     }
457     
458     private void activateTools()
459     {
460       if (!buttonUp.isDisposed())
461         buttonUp.setEnabled(true);
462       if (!buttonDown.isDisposed())
463         buttonDown.setEnabled(true);
464       if (!buttonDelete.isDisposed())
465         buttonDelete.setEnabled(true);
466       if (!buttonNew.isDisposed())
467         buttonNew.setEnabled(true);
468     }
469
470   /* (non-Javadoc)
471    * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
472    */

473   public void selectionChanged(IWorkbenchPart part, ISelection selection)
474   {
475     // TODO GEF-Bridge creates Exception
476
if (part instanceof AbstractEditor)
477     {
478       this.editor = (AbstractEditor) part;
479       this.mldc = editor.getMultiLayerDrawComponent();
480       editor.getOutlineEditDomain().getCommandStack().removeCommandStackListener(commandStackListener);
481       editor.getOutlineEditDomain().getCommandStack().addCommandStackListener(commandStackListener);
482     }
483     else if (!(getSite().getPage().getActiveEditor() instanceof AbstractEditor))
484     {
485       editor = null;
486       mldc = null;
487       if (layerComposite != null || !layerComposite.isDisposed())
488         deactivateTools(false);
489     }
490     refresh();
491   }
492
493     public void refresh()
494     {
495         // TODO instead of create new layerComposite, better remove old entries
496
if (scrollComposite != null || !scrollComposite.isDisposed())
497         {
498             button2Layer = new HashMap JavaDoc();
499             
500         layerComposite = new Composite(scrollComposite, SWT.NONE);
501         layerComposite.setSize(scrollComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
502         scrollComposite.setContent(layerComposite);
503 // FillLayout layerFillLayout = new FillLayout();
504
// layerFillLayout.type = SWT.VERTICAL;
505
// layerComposite.setLayout(layerFillLayout);
506
GridLayout layerGridLayout = new GridLayout();
507         layerComposite.setLayout(layerGridLayout);
508                     
509             if (mldc != null)
510             {
511                 for (int i = mldc.getDrawComponents().size()-1; i >= 0; --i)
512                 {
513                     DrawComponent dc = (DrawComponent) mldc.getDrawComponents().get(i);
514                     if (dc instanceof Layer) {
515                       Layer l = (Layer) dc;
516                       createLayerEntry(layerComposite, l);
517                     } else {
518                       LOGGER.debug("dc NOT instanceof Layer, but instanceof "+dc.getClass());
519                     }
520                 }
521                 
522                 if (mldc.getDrawComponents().size() <= 1)
523                   deactivateTools(true);
524                 else
525                   activateTools();
526                 
527                 scrollComposite.setMinSize(layerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
528                 layerComposite.pack();
529             }
530         }
531     }
532                 
533   public void dispose()
534   {
535     getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(this);
536     super.dispose();
537   }
538 }
539
Popular Tags