KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > ConfigureColumnsAction


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentation;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.IStructuredContentProvider;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.jface.viewers.Viewer;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.dialogs.ListSelectionDialog;
32 import org.eclipse.ui.texteditor.IUpdate;
33
34 /**
35  * Configures visible columns in an asynch tree viewer/
36  *
37  * @since 3.2
38  */

39 public class ConfigureColumnsAction extends Action implements IUpdate {
40     
41     private TreeModelViewer fViewer;
42     
43     class ColumnContentProvider implements IStructuredContentProvider {
44
45         /* (non-Javadoc)
46          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
47          */

48         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
49             return ((IColumnPresentation)inputElement).getAvailableColumns();
50         }
51
52         /* (non-Javadoc)
53          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
54          */

55         public void dispose() {
56         }
57
58         /* (non-Javadoc)
59          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
60          */

61         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
62         }
63         
64     }
65     
66     class ColumnLabelProvider extends LabelProvider {
67         
68         private Map JavaDoc fImages = new HashMap JavaDoc();
69
70         public Image getImage(Object JavaDoc element) {
71             ImageDescriptor imageDescriptor = fViewer.getColumnPresentation().getImageDescriptor((String JavaDoc)element);
72             if (imageDescriptor != null) {
73                 Image image = (Image) fImages.get(imageDescriptor);
74                 if (image == null) {
75                     image = imageDescriptor.createImage();
76                     fImages.put(imageDescriptor, image);
77                 }
78                 return image;
79             }
80             return null;
81         }
82
83         public String JavaDoc getText(Object JavaDoc element) {
84             return fViewer.getColumnPresentation().getHeader((String JavaDoc)element);
85         }
86
87         public void dispose() {
88             super.dispose();
89             Iterator JavaDoc iterator = fImages.values().iterator();
90             while (iterator.hasNext()) {
91                 Image image = (Image) iterator.next();
92                 image.dispose();
93             }
94             fImages.clear();
95         }
96         
97         
98         
99     }
100
101     public ConfigureColumnsAction(TreeModelViewer viewer) {
102         setText(ActionMessages.ConfigureColumnsAction_0);
103         setId(DebugUIPlugin.getUniqueIdentifier() + ".ConfigureColumnsAction"); //$NON-NLS-1$
104
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.CONFIGURE_COLUMNS_ACTION);
105         fViewer = viewer;
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.texteditor.IUpdate#update()
110      */

111     public void update() {
112         setEnabled(fViewer.isShowColumns());
113     }
114
115     /* (non-Javadoc)
116      * @see org.eclipse.jface.action.Action#run()
117      */

118     public void run() {
119         ListSelectionDialog dialog = new ListSelectionDialog(
120                 fViewer.getControl().getShell(),
121                 fViewer.getColumnPresentation(),
122                 new ColumnContentProvider(),
123                 new ColumnLabelProvider(),
124                 ActionMessages.ConfigureColumnsAction_1);
125         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.CONFIGURE_COLUMNS_DIALOG);
126         String JavaDoc[] visibleColumns = fViewer.getVisibleColumns();
127         List JavaDoc initialSelection = new ArrayList JavaDoc(visibleColumns.length);
128         for (int i = 0; i < visibleColumns.length; i++) {
129             initialSelection.add(visibleColumns[i]);
130         }
131         dialog.setTitle(ActionMessages.ConfigureColumnsAction_2);
132         dialog.setInitialElementSelections(initialSelection);
133         if (dialog.open() == Window.OK) {
134             Object JavaDoc[] result = dialog.getResult();
135             if (result.length == 0) {
136                 fViewer.setShowColumns(false);
137             } else {
138                 String JavaDoc[] ids = new String JavaDoc[result.length];
139                 System.arraycopy(result, 0, ids, 0, result.length);
140                 fViewer.resetColumnSizes(ids);
141                 fViewer.setVisibleColumns(ids);
142             }
143         }
144             
145     }
146     
147 }
148
Popular Tags