Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
VerticalTabbedPane.java
1 
16 package net.squiz.matrix.ui;
17 
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.awt.image.*;
21 
22 import javax.swing.*;
23 import javax.swing.event.*;
24 import javax.swing.tree.TreePath;
25 import javax.swing.plaf.metal.*;
26 
27 import net.squiz.matrix.assetmap.*;
28 import net.squiz.matrix.matrixtree.*;
29 import net.squiz.matrix.plaf.*;
30 import com.sun.java.swing.plaf.windows.*;
31 
32 public class VerticalTabbedPane extends JTabbedPane {
33 
34  private boolean tabsVertical;
35 
40  public VerticalTabbedPane() {
41  this(TOP);
42  }
43 
48  public VerticalTabbedPane(int tabPlacement) {
49  super(tabPlacement);
50  tabsVertical = (tabPlacement == LEFT || tabPlacement == RIGHT);
51 
52  // NdV: Quick hack to solve the color issue in J2SE 1.5
53  UIManager.put("TabbedPane.selected", MatrixLookAndFeel.PANEL_COLOUR);
54 
55  setUI(new VerticalTabbedPaneUI(tabsVertical));
56  // MM: need to do this outside of this class to keep it generic
57  /* addChangeListener( new ChangeListener() {
58  public void stateChanged(ChangeEvent evt) {
59  JTabbedPane pane = (JTabbedPane)evt.getSource();
60  MatrixTree currentTree = ( (BasicView) ( pane.getComponentAt(pane.getSelectedIndex()) ) ).getTree();
61 
62  for (int i = 0; i < pane.getTabCount(); i++) {
63  MatrixTree tree = ( (BasicView) ( pane.getComponentAt(i) ) ).getTree();
64  TreePath[] path = new TreePath[1];
65  path[0] = tree.getCuePath();
66  if (path != null) {
67  currentTree.startCueMode(path);
68  tree.stopCueMode();
69  break;
70  }
71  }
72  }
73  });*/
74  }
75 
81  public void addTab(String s, Icon icon, Component c) {
82  Icon textIcon = new VerticalTextIcon(s, getTabPlacement() == RIGHT);
83  VerticalCompoundIcon compoundIcon = new VerticalCompoundIcon(textIcon, icon, 0, 0);
84 
85  insertTab(
86  tabsVertical ? null : s,
87  tabsVertical ? compoundIcon : null,
88  c,
89  null,
90  getTabCount()
91  );
92 
93  BufferedImage image = new BufferedImage(
94  compoundIcon.getIconWidth(),
95  compoundIcon.getIconHeight(),
96  BufferedImage.TYPE_INT_ARGB_PRE
97  );
98  Graphics2D g = image.createGraphics();
99  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
100  compoundIcon.paintIcon(null, g, 0, 0);
101 
102  setDisabledIconAt(indexOfComponent(c), new ImageIcon(GrayFilter.createDisabledImage(image)));
103  }
104 
105  private class VerticalCompoundIcon extends CompoundIcon
106  {
107 
108  public VerticalCompoundIcon (Icon mainIcon, Icon decorator, int xAlignment, int yAlignment)
109  {
110  if (!isLegalValue(xAlignment, VALID_X)) {
111  throw new IllegalArgumentException(
112  "xAlignment must be LEFT, RIGHT or CENTER");
113  }
114  if (!isLegalValue(yAlignment, VALID_Y)) {
115  throw new IllegalArgumentException(
116  "yAlignment must be TOP, BOTTOM or CENTER");
117  }
118 
119  this.mainIcon = mainIcon;
120  this.decorator = decorator;
121  this.xAlignment = xAlignment;
122  this.yAlignment = yAlignment;
123  }
124 
131  public int getIconWidth() {
132  if (mainIcon.getIconWidth() > decorator.getIconWidth()) {
133  return mainIcon.getIconWidth() - 7;
134  } else {
135  return decorator.getIconWidth() - 7;
136  }
137  }
138 
144  public int getIconHeight() {
145  return mainIcon.getIconHeight() + decorator.getIconHeight();
146  }
147 
156  public void paintIcon(Component c, Graphics g, int x, int y) {
157 
158  int middleX = (x + x + getIconWidth()) / 2;
159  int middleY = (y + y + getIconHeight()) / 2;
160 
161  Graphics2D g2 = (Graphics2D) g.create();
162 
163  int mainIconX = middleX - (mainIcon.getIconWidth() / 2);
164  mainIcon.paintIcon(c, g2, mainIconX, y);
165 
166  g2.rotate(Math.toRadians(-90), middleX, middleY);
167 
168  int decoratorX = middleX - (getIconHeight() / 2);
169  int decoratorY = middleY - (decorator.getIconHeight() / 2);
170 
171  decorator.paintIcon(
172  null,
173  g2,
174  decoratorX + (mainIcon.getIconWidth() / 2) + 2,
175  decoratorY
176  );
177 
178  g2.dispose();
179  }
180  }
181 
182  private class VerticalTextIcon implements Icon {
183  private String tabText;
184  private boolean clockwise;
185 
191  public VerticalTextIcon(String tabText, boolean clockwise) {
192  this.tabText = tabText;
193  this.clockwise = clockwise;
194  }
195 
204  public void paintIcon(Component c, Graphics g, int x, int y) {
205  Graphics2D g2 = (Graphics2D) g;
206 
207  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
208 
209  // rotate the graphics 90 degress and draw the string
210  // clockwise for the right, anti-clockwise for the left
211  g2.rotate(Math.toRadians(clockwise ? 90 : -90), x, y);
212  g2.setFont((Font)UIManager.get("VerticalTextIcon.font"));
213 
214  g2.drawString(
215  tabText,
216  x - (clockwise ? 0 : getIconHeight()) + 8,
217  y + (clockwise ? 0 : getIconWidth()));
218  g2.rotate (Math.toRadians(clockwise ? - 90 : 90), x, y
219  );
220  }
221 
226  public int getIconWidth() {
227  return 8;
228  }
229 
234  public int getIconHeight() {
235  return getFontMetrics(getFont()).stringWidth(tabText) + 20;
236  }
237  }
238 }