KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > util > RCPUtil


1 /*
2  * Created on Apr 22, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.rcp.util;
7
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.widgets.Button;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.swt.widgets.MessageBox;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.ui.IWorkbenchPage;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.eclipse.ui.PartInitException;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.internal.Workbench;
20
21 import com.nightlabs.base.NLBasePlugin;
22
23 /**
24  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
25  *
26  */

27 public class RCPUtil {
28
29     /**
30      * Recursively sets the enabled flag for the given Composite and all its children.
31      *
32      * @param comp The parent control
33      * @param enabled The enabled flag to set
34      */

35     public static void setControlEnabledRecursive(Composite comp, boolean enabled) {
36         comp.setEnabled(enabled);
37         Control[] children = comp.getChildren();
38         for (int i = 0; i < children.length; i++) {
39             if (children[i] instanceof Composite)
40                 setControlEnabledRecursive((Composite)children[i], enabled);
41         }
42     }
43
44     /**
45      * Sets the given button selected if it is contained in the given parent Composite,
46      * and deselects all other buttons in this Composite
47      *
48      * @param parent the parent Composite
49      * @param button a Button with style Param SWT.TOGGLE or SWT.RADIO which should be selected
50      */

51     public static void setButtonSelected(Composite parent, Button button)
52     {
53       button.setSelection(true);
54       Control[] children = parent.getChildren();
55       for (int i = 0; i < children.length; i++) {
56         if (!children[i].equals(button)) {
57           if ( (((children[i].getStyle() & SWT.TOGGLE) != 0) || ((children[i].getStyle() & SWT.RADIO) != 0))
58               && (children[i] instanceof Button) )
59           {
60             ((Button)children[i]).setSelection(false);
61           }
62         }
63       }
64     }
65     
66     /**
67      * Returns wether the ViewPart with the given id is currently visble in
68      * one of the pages of the active Workbench window. Will also return
69      * true when the page-book containing this view is minimized.
70      *
71      * @param viewID The id of the view to be queried
72      * @return Wether the view is visible
73      */

74     public static boolean isViewVisible(String JavaDoc viewID) {
75 // IWorkbenchPage[] pages = Workbench.getInstance().getActiveWorkbenchWindow().getPages();
76
IWorkbenchPage[] pages = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPages();
77         for (int i = 0; i < pages.length; i++) {
78             IWorkbenchPart part = pages[i].findView(viewID);
79             if (part != null) {
80                 return isPartVisible(part);
81             }
82         }
83         return false;
84     }
85
86     /**
87      * Returns wether the given IWorkbenchPart is currently visble in
88      * one of the pages of the active Workbench window. Will also return
89      * true when the page-book containing this view is minimized.
90      *
91      * @param viewID The id of the view to be queried
92      * @return Wether the view is visible
93      */

94     public static boolean isPartVisible(IWorkbenchPart part) {
95         boolean visible = false;
96         IWorkbenchPage[] pages = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPages();
97         for (int i = 0; i < pages.length; i++) {
98             if (part != null)
99                 if (pages[i].isPartVisible(part)){
100                     visible = true;
101                 }
102         }
103         return visible;
104     }
105     
106     /**
107      * Shows the view with the given viewID in all workbench-pages
108      *
109      * @param viewID The id of the view to be queried
110      * @return Wether the view is visible
111      */

112     public static IWorkbenchPart showView(String JavaDoc viewID) {
113         IWorkbenchPage[] pages = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPages();
114         for (int i = 0; i < pages.length; i++) {
115             IWorkbenchPart view = null;
116             try { view = pages[0].showView(viewID); } catch (PartInitException e) { throw new RuntimeException JavaDoc(e); }
117             if (view != null)
118                 return view;
119         }
120         return null;
121     }
122     
123     /**
124      * Shows the view with the given viewID and
125      * gives it focus.
126      *
127      * @param viewID The id of the view to be queried
128      * @return Wether the view is visible
129      */

130     public static IWorkbenchPart activateView(String JavaDoc viewID) {
131         IWorkbenchPage[] pages = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPages();
132         for (int i = 0; i < pages.length; i++) {
133             IWorkbenchPart view = null;
134             try { view = pages[0].showView(viewID); } catch (PartInitException e) { throw new RuntimeException JavaDoc(e); }
135             if (view != null) {
136                 pages[0].activate(view);
137                 return view;
138             }
139         }
140         return null;
141     }
142     
143     public static Shell getWorkbenchShell() {
144         return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
145     }
146     
147     public static int showErrorDialog(String JavaDoc message, int buttonStyle)
148     {
149         MessageBox errorDialog = new MessageBox(getWorkbenchShell(), SWT.ICON_ERROR | buttonStyle);
150         errorDialog.setMessage(message);
151         errorDialog.setText(NLBasePlugin.getResourceString("action.openfile.error.title"));
152         return errorDialog.open();
153     }
154     
155     public static int showErrorDialog(String JavaDoc message)
156     {
157         return showErrorDialog(message, SWT.OK);
158     }
159     
160   public static int showConfirmOverwriteDialog(String JavaDoc fileName)
161   {
162         MessageBox errorDialog = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.ICON_INFORMATION | SWT.OK | SWT.CANCEL);
163         errorDialog.setMessage(
164                 NLBasePlugin.getResourceString("action.openfile.confirmoverwrite.message1")
165                 + " " + fileName + " " +
166                 NLBasePlugin.getResourceString("action.openfile.confirmoverwrite.message2"));
167         errorDialog.setText(NLBasePlugin.getResourceString("action.openfile.confirmoverwrite.title"));
168         return errorDialog.open();
169   }
170     
171 }
172
Popular Tags