1 19 20 package org.netbeans.core.windows.view.ui.slides; 21 22 import java.awt.Component ; 23 import java.awt.Graphics2D ; 24 import java.awt.GraphicsConfiguration ; 25 import java.awt.GraphicsEnvironment ; 26 import java.awt.Image ; 27 import java.awt.Point ; 28 import java.awt.Rectangle ; 29 import java.awt.Shape ; 30 import java.awt.Toolkit ; 31 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.awt.image.BufferedImage ; 34 import java.beans.PropertyChangeListener ; 35 import java.util.ArrayList ; 36 import java.util.Collections ; 37 import java.util.List ; 38 import javax.swing.Action ; 39 import javax.swing.DefaultSingleSelectionModel ; 40 import javax.swing.Icon ; 41 import javax.swing.ImageIcon ; 42 import javax.swing.JLabel ; 43 import javax.swing.SingleSelectionModel ; 44 import javax.swing.event.ChangeEvent ; 45 import javax.swing.event.ChangeListener ; 46 import org.netbeans.core.windows.Constants; 47 import org.netbeans.core.windows.WindowManagerImpl; 48 import org.netbeans.core.windows.actions.ActionUtils; 49 import org.netbeans.core.windows.view.ui.Tabbed; 50 import org.netbeans.swing.tabcontrol.SlideBarDataModel; 51 import org.netbeans.swing.tabcontrol.TabData; 52 import org.netbeans.swing.tabcontrol.TabDataModel; 53 import org.netbeans.swing.tabcontrol.TabbedContainer; 54 import org.openide.windows.TopComponent; 55 56 63 public final class TabbedSlideAdapter implements Tabbed { 64 65 66 private TabDataModel dataModel; 67 68 private SingleSelectionModel selModel; 69 70 private SlideBar slideBar; 71 72 private List <ActionListener > actionListeners; 73 74 private List <ChangeListener > selectionListeners; 75 76 private final ChangeEvent selectionEvt = new ChangeEvent (this); 77 78 79 public TabbedSlideAdapter(String side) { 80 dataModel = new SlideBarDataModel.Impl(); 81 setSide(side); 82 selModel = new DefaultSingleSelectionModel (); 83 slideBar = new SlideBar(this, (SlideBarDataModel)dataModel, selModel); 84 } 85 86 public void requestAttention (TopComponent tc) { 87 slideBar.setBlinking(tc, true); 88 } 89 90 public void cancelRequestAttention (TopComponent tc) { 91 slideBar.setBlinking(tc, false); 92 } 93 94 95 private void setSide (String side) { 96 int orientation = SlideBarDataModel.WEST; 97 if (Constants.LEFT.equals(side)) { 98 orientation = SlideBarDataModel.WEST; 99 } else if (Constants.RIGHT.equals(side)) { 100 orientation = SlideBarDataModel.EAST; 101 } else if (Constants.BOTTOM.equals(side)) { 102 orientation = SlideBarDataModel.SOUTH; 103 } 104 ((SlideBarDataModel)dataModel).setOrientation(orientation); 105 } 106 107 public final synchronized void addActionListener(ActionListener listener) { 108 if (actionListeners == null) { 109 actionListeners = new ArrayList <ActionListener >(); 110 } 111 actionListeners.add(listener); 112 } 113 114 119 public final synchronized void removeActionListener(ActionListener listener) { 120 if (actionListeners != null) { 121 actionListeners.remove(listener); 122 if (actionListeners.isEmpty()) { 123 actionListeners = null; 124 } 125 } 126 } 127 128 final void postActionEvent(ActionEvent event) { 129 List <ActionListener > list; 130 synchronized (this) { 131 if (actionListeners == null) 132 return; 133 list = Collections.unmodifiableList(actionListeners); 134 } 135 for (int i = 0; i < list.size(); i++) { 136 list.get(i).actionPerformed(event); 137 } 138 } 139 140 public void addChangeListener(ChangeListener listener) { 141 if (selectionListeners == null) { 142 selectionListeners = new ArrayList <ChangeListener >(); 143 } 144 selectionListeners.add(listener); 145 } 146 147 public void removeChangeListener(ChangeListener listener) { 148 if (selectionListeners != null) { 149 selectionListeners.remove(listener); 150 if (selectionListeners.isEmpty()) { 151 selectionListeners = null; 152 } 153 } 154 } 155 156 final void postSelectionEvent() { 157 List <ChangeListener > list; 158 synchronized (this) { 159 if (selectionListeners == null) 160 return; 161 list = Collections.unmodifiableList(selectionListeners); 162 } 163 for (int i = 0; i < list.size(); i++) { 164 list.get(i).stateChanged(selectionEvt); 165 } 166 } 167 168 public void addPropertyChangeListener(String name, PropertyChangeListener listener) { 169 slideBar.addPropertyChangeListener(name, listener); 170 } 171 172 public void removePropertyChangeListener(String name, PropertyChangeListener listener) { 173 slideBar.removePropertyChangeListener(name, listener); 174 } 175 176 public void addTopComponent(String name, Icon icon, TopComponent tc, String toolTip) { 177 dataModel.addTab(dataModel.size(), new TabData(tc, icon, name, toolTip)); 178 } 179 180 public TopComponent getSelectedTopComponent() { 181 int index = selModel.getSelectedIndex(); 182 return index < 0 ? null : (TopComponent)dataModel.getTab(index).getComponent(); 183 } 184 185 public TopComponent getTopComponentAt(int index) { 186 return (TopComponent)dataModel.getTab(index).getComponent(); 187 } 188 189 public TopComponent[] getTopComponents() { 190 int size = dataModel.size(); 191 TopComponent[] result = new TopComponent[size]; 192 for (int i=0; i < size; i++) { 193 result[i] = (TopComponent) dataModel.getTab(i).getComponent(); 194 } 195 return result; 196 } 197 198 public void setActive(boolean active) { 199 slideBar.setActive(active); 200 } 201 202 public void setIconAt(int index, Icon icon) { 203 dataModel.setIcon(index, icon); 204 } 205 206 public void setTitleAt(int index, String title) { 207 dataModel.setText(index, title); 208 } 209 210 public void setToolTipTextAt(int index, String toolTip) { 211 } 213 214 public void setTopComponents(TopComponent[] tcs, TopComponent selected) { 215 TabData[] data = new TabData[tcs.length]; 216 int toSelect=-1; 217 for(int i = 0; i < tcs.length; i++) { 218 TopComponent tc = tcs[i]; 219 Image icon = tc.getIcon(); 220 String displayName = WindowManagerImpl.getInstance().getTopComponentDisplayName(tc); 221 data[i] = new TabData( 222 tc, 223 icon == null ? null : new ImageIcon (icon), 224 displayName == null ? "" : displayName, tc.getToolTipText()); 226 if (selected == tcs[i]) { 227 toSelect = i; 228 } 229 } 230 231 dataModel.setTabs(data); 232 setSelectedComponent(selected); 233 } 234 235 public int getTabCount() { 236 return dataModel.size(); 237 } 238 239 public int indexOf(Component tc) { 240 int size = dataModel.size(); 241 for (int i=0; i < size; i++) { 242 if (tc == dataModel.getTab(i).getComponent()) return i; 243 } 244 return -1; 245 } 246 247 public void insertComponent(String name, Icon icon, Component comp, String toolTip, int position) { 248 dataModel.addTab(position, new TabData(comp, icon, name, toolTip)); 249 } 250 251 public void removeComponent(Component comp) { 252 int i = indexOf(comp); 253 dataModel.removeTab(i); 254 } 255 256 public void setSelectedComponent(Component comp) { 257 int newIndex = indexOf(comp); 258 if (selModel.getSelectedIndex() != newIndex) { 259 selModel.setSelectedIndex(newIndex); 260 } 261 if (comp instanceof TopComponent) { 262 TopComponent tc = (TopComponent) comp; 264 tc.cancelRequestAttention(); 265 } 266 } 267 268 public int tabForCoordinate(Point p) { 269 return slideBar.tabForCoordinate(p.x, p.y); 270 } 271 272 public Component getComponent() { 273 return slideBar; 274 } 275 276 277 278 public Object getConstraintForLocation(Point location, boolean attachingPossible) { 279 int tab = slideBar.nextTabForCoordinate(location.x, location.y); 280 return Integer.valueOf(tab); 281 } 282 283 public Shape getIndicationForLocation(Point location, TopComponent startingTransfer, Point startingPoint, boolean attachingPossible) { 284 285 int nextTab = slideBar.nextTabForCoordinate(location.x, location.y); 287 SlideBarDataModel sbdm = (SlideBarDataModel)dataModel; 288 if (getTabCount() != 0) { 289 if (nextTab == 0) { 290 Rectangle rect = getTabBounds(0); 291 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) { 292 rect.x = 0; 293 rect.width = rect.width / 2; 294 } else { 295 rect.y = 0; 296 rect.height = rect.height / 2; 297 } 298 return rect; 299 } else if (nextTab < getTabCount()) { 300 Rectangle rect1 = getTabBounds(nextTab - 1); 301 Rectangle rect2 = getTabBounds(nextTab); 302 Rectangle result = new Rectangle (); 303 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) { 304 result.y = rect1.y; 305 result.height = rect1.height; 306 result.x = rect1.x + (rect1.width / 2); 307 result.width = rect2.x + (rect2.width / 2) - result.x; 308 } else { 309 result.x = rect1.x; 310 result.width = rect1.width; 311 result.y = rect1.y + (rect1.height / 2); 312 result.height = rect2.y + (rect2.height / 2) - result.y; 313 } 314 return result; 315 } else if (nextTab == getTabCount()) { 316 Rectangle rect = getTabBounds(getTabCount() - 1); 317 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) { 318 rect.x = rect.x + rect.width; 319 } else { 320 rect.y = rect.y + rect.height; 321 } 322 return rect; 323 } 324 } 325 Rectangle rect = slideBar.getBounds(); 326 if (sbdm.getOrientation() == SlideBarDataModel.SOUTH) { 327 return new Rectangle (10, 0, 50, 20); 328 } 329 return new Rectangle (0, 10, 20, 50); 330 } 331 332 public Image createImageOfTab(int tabIndex) { 333 TabData dt = slideBar.getModel().getTab(tabIndex); 334 if (dt.getComponent() instanceof TopComponent) { 335 336 JLabel lbl = new JLabel (dt.getText()); 337 int width = lbl.getFontMetrics(lbl.getFont()).stringWidth(dt.getText()); 338 int height = lbl.getFontMetrics(lbl.getFont()).getHeight(); 339 Image img = ((TopComponent)dt.getComponent()).getIcon(); 340 lbl.setIcon(new ImageIcon (img)); 341 width = width + (img.getWidth(null) == -1 ? 16 : img.getWidth(null)) + 6; 342 height = Math.max(height + 5, img.getHeight(null) == -1 ? 21 : 5 + img.getHeight(null)); 343 344 GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment() 345 .getDefaultScreenDevice().getDefaultConfiguration(); 346 347 348 BufferedImage image = config.createCompatibleImage(width, height); 349 Graphics2D g = image.createGraphics(); 350 g.setColor(lbl.getForeground()); 351 g.setFont(lbl.getFont()); 352 g.drawImage(img, 0, 0, null); 353 g.drawString(dt.getText(), 18, height / 2); 354 355 return image; 356 } 357 358 return null; 359 } 360 361 362 public Action [] getPopupActions(Action [] defaultActions, int tabIndex) { 363 boolean isMDI = WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_JOINED; 364 Action [] result = new Action [defaultActions.length + (isMDI ? 1 : 0)]; 365 System.arraycopy(defaultActions, 0, result, 0, defaultActions.length); 366 if (isMDI) { 367 result[defaultActions.length] = 368 new ActionUtils.AutoHideWindowAction(slideBar, tabIndex, true); 369 } 370 return result; 371 } 372 373 public Rectangle getTabBounds(int tabIndex) { 374 return slideBar.getTabBounds(tabIndex); 375 } 376 377 } 378 379 | Popular Tags |