KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > base > entitylist > DefaultEntityListView


1 /*
2  * Created on 05.11.2004
3  *
4  */

5 package com.nightlabs.base.entitylist;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionChangedListener;
12 import org.eclipse.jface.viewers.SelectionChangedEvent;
13 import org.eclipse.jface.viewers.TableViewer;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Combo;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.TabFolder;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
24 import org.eclipse.ui.IPartListener2;
25 import org.eclipse.ui.IWorkbenchPartReference;
26
27 /**
28  * @author Niklas Schiffler <nick@nightlabs.de>
29  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
30  *
31  */

32 public abstract class DefaultEntityListView
33     extends AbstractEntityListView
34     implements SelectionListener, EntityManagementViewStateChangedListener
35 {
36   private TableViewer viewer;
37   private Combo filterCombo;
38   
39   private EntityListContentProvider contentProvider;
40   private ArrayList JavaDoc selections;
41   private class EntitySelectionListener implements ISelectionChangedListener
42     {
43     private DefaultEntityListView mother;
44         private ISelectionChangedListener listChangeListener;
45         
46     public EntitySelectionListener(DefaultEntityListView source)
47     {
48         mother = source;
49             this.listChangeListener = mother.createListChangeListener();
50     }
51     public void selectionChanged(SelectionChangedEvent event)
52     {
53       ISelection selection = event.getSelection();
54       if(!selection.isEmpty())
55         mother.entityListSelected(mother.getSelectedListID());
56             
57             if (listChangeListener != null)
58                 listChangeListener.selectionChanged(event);
59     }
60   }
61   private class EntityManagementViewListener implements IPartListener2
62     {
63     private DefaultEntityListView elv;
64     public EntityManagementViewListener(DefaultEntityListView elv)
65     {
66         this.elv = elv;
67     }
68     
69     public void partActivated(IWorkbenchPartReference partRef)
70     {
71         Object JavaDoc o = partRef.getPart(false);
72         if(o instanceof EntityManagementView)
73             elv.viewActivated(partRef.getId());
74     }
75
76     public void partBroughtToTop(IWorkbenchPartReference partRef)
77     {
78 // System.out.println(partRef.getId() + " : brought to top");
79
}
80
81     public void partClosed(IWorkbenchPartReference partRef)
82     {
83     }
84
85     public void partDeactivated(IWorkbenchPartReference partRef)
86     {
87     }
88
89     public void partOpened(IWorkbenchPartReference partRef)
90     {
91 // System.out.println(partRef.getId() + " : opened");
92
}
93
94     public void partHidden(IWorkbenchPartReference partRef)
95     {
96     }
97
98     public void partVisible(IWorkbenchPartReference partRef)
99     {
100 // System.out.println(partRef.getId() + " : visible");
101
}
102
103     public void partInputChanged(IWorkbenchPartReference partRef)
104     {
105     }
106
107   }
108   private EntitySelectionListener eSelectionListener;
109   
110   public DefaultEntityListView()
111   {
112     super();
113     selections = new ArrayList JavaDoc();
114   }
115
116   public void createEntityListControl(Composite parent) {
117     GridLayout gridLayout = new GridLayout();
118     gridLayout.numColumns = 2;
119     
120     TabFolder composite = new TabFolder(parent, SWT.NULL);
121     composite.setLayout(gridLayout);
122     
123     filterCombo = new Combo(composite, SWT.READ_ONLY);
124     filterCombo.addSelectionListener(this);
125     
126     viewer = new TableViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.MULTI);
127     eSelectionListener = new EntitySelectionListener(this);
128     viewer.addSelectionChangedListener(eSelectionListener);
129     getSite().setSelectionProvider(viewer);
130
131     contentProvider = new EntityListContentProvider();
132     viewer.setContentProvider(contentProvider);
133     
134     
135     GridData tgd = new GridData(GridData.FILL_BOTH);
136     tgd.horizontalSpan = 2;
137     tgd.verticalSpan = 1;
138     
139     Table t = viewer.getTable();
140     t.setHeaderVisible(true);
141     t.setLinesVisible(true);
142     t.setLayoutData(tgd);
143     }
144
145   public void setFocus()
146   {
147     filterCombo.setFocus();
148   }
149     
150     private static Collection JavaDoc EMPTY_COLLECTION = new ArrayList JavaDoc();
151     
152   public void refresh()
153   {
154     String JavaDoc key = (String JavaDoc)selections.get(filterCombo.getSelectionIndex());
155     EntityList el = (EntityList)getEntityLists().get(key);
156     if(el != null)
157     {
158             viewer.setInput(EMPTY_COLLECTION);
159         viewer.setLabelProvider(el.getLabelProvider());
160             TableColumn[] columns = viewer.getTable().getColumns();
161             for (int i = 0; i < columns.length; i++) {
162                 columns[i].dispose();
163             }
164             el.addTableColumns(viewer.getTable());
165             viewer.getTable().layout();
166         viewer.setInput(el.getEntities());
167         viewer.refresh();
168     }
169   }
170
171   public String JavaDoc getSelectedListID()
172   {
173     return (String JavaDoc)selections.get(filterCombo.getSelectionIndex());
174   }
175     
176     
177   
178
179     protected void entityListAdded(EntityList el) {
180         filterCombo.add(el.getLabel());
181         selections.add(el.getID());
182         
183     }
184
185
186
187     public void setSelectedEntityList(String JavaDoc entityListID) {
188         if(filterCombo.getSelectionIndex() != selections.indexOf(entityListID))
189             filterCombo.select(selections.indexOf(entityListID));
190         refresh();
191     }
192
193
194
195     // combo selection - begin
196
public void widgetSelected(SelectionEvent arg0)
197   {
198     Combo c = (Combo)arg0.getSource();
199         String JavaDoc entityListID = (String JavaDoc)selections.get(c.getSelectionIndex());
200         setSelectedEntityList(entityListID);
201         showManagerViews(entityListID);
202   }
203
204   public void widgetDefaultSelected(SelectionEvent arg0)
205   {
206   }
207   // combo selection - end
208

209   public void dispose()
210   {
211     super.dispose();
212     viewer.removeSelectionChangedListener(eSelectionListener);
213   }
214 }
215
Popular Tags