1 19 20 21 package org.netbeans.core.windows.view.ui; 22 23 24 import java.lang.reflect.Field ; 25 import org.netbeans.core.windows.Constants; 26 import org.netbeans.core.windows.ModeImpl; 27 import org.netbeans.core.windows.WindowManagerImpl; 28 import org.netbeans.core.windows.view.ModeContainer; 29 import org.netbeans.core.windows.view.ModeView; 30 import org.netbeans.core.windows.view.dnd.TopComponentDroppable; 31 import org.netbeans.core.windows.view.dnd.WindowDnDManager; 32 import org.openide.util.Utilities; 33 import org.openide.windows.TopComponent; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.util.Arrays ; 38 39 40 46 public abstract class AbstractModeContainer implements ModeContainer { 47 48 49 protected final ModeView modeView; 50 51 protected final TabbedHandler tabbedHandler; 52 53 protected final WindowDnDManager windowDnDManager; 55 56 private final int kind; 58 59 60 public AbstractModeContainer(ModeView modeView, WindowDnDManager windowDnDManager, int kind) { 61 this.modeView = modeView; 62 this.windowDnDManager = windowDnDManager; 63 this.kind = kind; 64 this.tabbedHandler = new TabbedHandler(modeView, kind, createTabbed()); 65 } 66 67 68 public ModeView getModeView() { 69 return modeView; 70 } 71 72 73 public Component getComponent() { 74 return getModeComponent(); 75 } 76 77 protected abstract Component getModeComponent(); 78 79 protected abstract Tabbed createTabbed(); 80 81 public void addTopComponent(TopComponent tc) { 82 tabbedHandler.addTopComponent(tc, kind); 83 } 84 85 public void removeTopComponent(TopComponent tc) { 86 tabbedHandler.removeTopComponent(tc); 87 88 TopComponent selected = tabbedHandler.getSelectedTopComponent(); 89 updateTitle(selected == null 90 ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(selected)); } 92 93 public void setSelectedTopComponent(TopComponent tc) { 94 tabbedHandler.setSelectedTopComponent(tc); 95 96 updateTitle(WindowManagerImpl.getInstance().getTopComponentDisplayName(tc)); 97 } 98 99 public void setTopComponents(TopComponent[] tcs, TopComponent selected) { 100 if (!Arrays.equals(tcs, getTopComponents())) { 102 tabbedHandler.setTopComponents(tcs, selected); 103 updateTitle(WindowManagerImpl.getInstance().getTopComponentDisplayName(selected)); 104 } else { 105 setSelectedTopComponent(selected); 108 } 109 } 110 111 protected abstract void updateTitle(String title); 112 113 protected abstract void updateActive(boolean active); 114 115 116 public TopComponent getSelectedTopComponent() { 117 return tabbedHandler.getSelectedTopComponent(); 118 } 119 120 public void setActive(boolean active) { 121 updateActive(active); 122 123 TopComponent selected = tabbedHandler.getSelectedTopComponent(); 124 updateTitle(selected == null 125 ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(selected)); 127 tabbedHandler.setActive(active); 128 } 129 130 public void focusSelectedTopComponent() { 131 final TopComponent selectedTopComponent = tabbedHandler.getSelectedTopComponent(); 133 134 if (selectedTopComponent == null) { 135 return; 136 } 137 138 Window oldFocusedW = FocusManager.getCurrentManager().getFocusedWindow(); 139 Window newFocusedW = SwingUtilities.getWindowAncestor(selectedTopComponent); 140 141 if (newFocusedW != null && newFocusedW.equals(oldFocusedW)) { 142 if (!Utilities.isMac()) { 144 selectedTopComponent.requestFocusInWindow(); 145 } else { 146 SwingUtilities.invokeLater(new Runnable () { 149 public void run() { 150 try { 156 Field fld = KeyboardFocusManager.class.getDeclaredField("focusOwner"); fld.setAccessible(true); 158 fld.set(KeyboardFocusManager.getCurrentKeyboardFocusManager(), null); 159 } catch (IllegalArgumentException ex) { 160 ex.printStackTrace(); 161 } catch (SecurityException ex) { 162 ex.printStackTrace(); 163 } catch (NoSuchFieldException ex) { 164 ex.printStackTrace(); 165 } catch (IllegalAccessException ex) { 166 ex.printStackTrace(); 167 } 168 selectedTopComponent.requestFocusInWindow(); 170 } 171 }); 172 } 173 } else { 174 selectedTopComponent.requestFocus(); 176 } 177 } 178 179 180 public TopComponent[] getTopComponents() { 181 return tabbedHandler.getTopComponents(); 182 } 183 184 public void updateName(TopComponent tc) { 185 TopComponent selected = getSelectedTopComponent(); 186 if(tc == selected) { 187 updateTitle(tc == null 188 ? "" : WindowManagerImpl.getInstance().getTopComponentDisplayName(tc)); } 190 191 tabbedHandler.topComponentNameChanged(tc, kind); 192 } 193 194 public void updateToolTip(TopComponent tc) { 195 tabbedHandler.topComponentToolTipChanged(tc); 196 } 197 198 public void updateIcon(TopComponent tc) { 199 tabbedHandler.topComponentIconChanged(tc); 200 } 201 202 protected int getKind() { 204 return kind; 205 } 206 207 protected Shape getIndicationForLocation(Point location) { 210 return tabbedHandler.getIndicationForLocation(location, 211 windowDnDManager.getStartingTransfer(), 212 windowDnDManager.getStartingPoint(), 213 isAttachingPossible()); 214 } 215 216 protected Object getConstraintForLocation(Point location) { 217 return tabbedHandler.getConstraintForLocation(location, isAttachingPossible()); 218 } 219 220 protected abstract boolean isAttachingPossible(); 221 222 protected ModeView getDropModeView() { 223 return modeView; 224 } 225 226 protected Component getDropComponent() { 227 return tabbedHandler.getComponent(); 228 } 229 230 protected abstract TopComponentDroppable getModeDroppable(); 231 232 protected boolean canDrop(TopComponent transfer) { 233 if(Constants.SWITCH_MODE_ADD_NO_RESTRICT 234 || WindowManagerImpl.getInstance().isTopComponentAllowedToMoveAnywhere(transfer)) { 235 return true; 236 } 237 238 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(transfer); 239 int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR; 240 241 boolean isNonEditor = kind == Constants.MODE_KIND_VIEW || kind == Constants.MODE_KIND_SLIDING; 242 boolean thisIsNonEditor = this.kind == Constants.MODE_KIND_VIEW || this.kind == Constants.MODE_KIND_SLIDING; 243 return isNonEditor == thisIsNonEditor; 244 } 245 248 } 249 250 | Popular Tags |