1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.widgets.*; 17 18 63 public class ControlEditor { 64 65 69 public int horizontalAlignment = SWT.CENTER; 70 71 76 public boolean grabHorizontal = false; 77 78 83 public int minimumWidth = 0; 84 85 89 public int verticalAlignment = SWT.CENTER; 90 91 96 public boolean grabVertical = false; 97 98 103 public int minimumHeight = 0; 104 105 Composite parent; 106 Control editor; 107 private boolean hadFocus; 108 private Listener controlListener; 109 private Listener scrollbarListener; 110 111 private final static int [] EVENTS = {SWT.KeyDown, SWT.KeyUp, SWT.MouseDown, SWT.MouseUp, SWT.Resize}; 112 118 public ControlEditor (Composite parent) { 119 this.parent = parent; 120 121 controlListener = new Listener() { 122 public void handleEvent(Event e) { 123 layout (); 124 } 125 }; 126 for (int i=0; i<EVENTS.length; i++) { 127 parent.addListener (EVENTS [i], controlListener); 128 } 129 130 scrollbarListener = new Listener() { 131 public void handleEvent(Event e) { 132 scroll (e); 133 } 134 }; 135 ScrollBar hBar = parent.getHorizontalBar (); 136 if (hBar != null) hBar.addListener (SWT.Selection, scrollbarListener); 137 ScrollBar vBar = parent.getVerticalBar (); 138 if (vBar != null) vBar.addListener (SWT.Selection, scrollbarListener); 139 } 140 Rectangle computeBounds () { 141 Rectangle clientArea = parent.getClientArea(); 142 Rectangle editorRect = new Rectangle(clientArea.x, clientArea.y, minimumWidth, minimumHeight); 143 144 if (grabHorizontal) 145 editorRect.width = Math.max(clientArea.width, minimumWidth); 146 147 if (grabVertical) 148 editorRect.height = Math.max(clientArea.height, minimumHeight); 149 150 switch (horizontalAlignment) { 151 case SWT.RIGHT: 152 editorRect.x += clientArea.width - editorRect.width; 153 break; 154 case SWT.LEFT: 155 break; 157 default: 158 editorRect.x += (clientArea.width - editorRect.width)/2; 160 } 161 162 switch (verticalAlignment) { 163 case SWT.BOTTOM: 164 editorRect.y += clientArea.height - editorRect.height; 165 break; 166 case SWT.TOP: 167 break; 169 default : 170 editorRect.y += (clientArea.height - editorRect.height)/2; 172 } 173 174 175 return editorRect; 176 177 } 178 182 public void dispose () { 183 if (parent != null && !parent.isDisposed()) { 184 for (int i=0; i<EVENTS.length; i++) { 185 parent.removeListener (EVENTS [i], controlListener); 186 } 187 ScrollBar hBar = parent.getHorizontalBar (); 188 if (hBar != null) hBar.removeListener (SWT.Selection, scrollbarListener); 189 ScrollBar vBar = parent.getVerticalBar (); 190 if (vBar != null) vBar.removeListener (SWT.Selection, scrollbarListener); 191 } 192 193 parent = null; 194 editor = null; 195 hadFocus = false; 196 controlListener = null; 197 scrollbarListener = null; 198 } 199 204 public Control getEditor () { 205 return editor; 206 } 207 214 public void layout () { 215 if (editor == null || editor.isDisposed()) return; 216 if (editor.getVisible ()) { 217 hadFocus = editor.isFocusControl(); 218 } editor.setBounds (computeBounds ()); 222 if (hadFocus) { 223 if (editor == null || editor.isDisposed()) return; 224 editor.setFocus (); 225 } 226 } 227 void scroll (Event e) { 228 if (editor == null || editor.isDisposed()) return; 229 layout(); 230 } 231 239 public void setEditor (Control editor) { 240 241 if (editor == null) { 242 this.editor = null; 245 return; 246 } 247 248 this.editor = editor; 249 layout(); 250 if (this.editor == null || this.editor.isDisposed()) return; 251 editor.setVisible(true); 252 } 253 } 254 | Popular Tags |