1 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 27 public class RCPUtil { 28 29 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 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 74 public static boolean isViewVisible(String viewID) { 75 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 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 112 public static IWorkbenchPart showView(String 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 (e); } 117 if (view != null) 118 return view; 119 } 120 return null; 121 } 122 123 130 public static IWorkbenchPart activateView(String 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 (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 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 message) 156 { 157 return showErrorDialog(message, SWT.OK); 158 } 159 160 public static int showConfirmOverwriteDialog(String 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 |