001: package org.jvnet.substance.netbeans;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.border.EmptyBorder;
007: import javax.swing.plaf.ComponentUI;
008: import org.jvnet.lafwidget.utils.RenderingUtils;
009: import org.jvnet.substance.*;
010: import org.jvnet.substance.theme.SubstanceTheme;
011: import org.jvnet.substance.utils.MemoryAnalyzer;
012: import org.netbeans.swing.tabcontrol.TabDisplayer;
013: import org.netbeans.swing.tabcontrol.plaf.*;
014:
015: /**
016: * UI delegate for tab displayer. This class is based on code from
017: * <code>org.netbeans.swing.tabcontrol.plaf.MetalEditorTabDisplayerUI</code>
018: * from NetBeans source base.
019: *
020: * @author Kirill Grouchnikov
021: */
022: public class SubstanceEditorTabDisplayerUI extends
023: BasicScrollingTabDisplayerUI {
024:
025: private Rectangle scratch = new Rectangle();
026:
027: /**
028: * Simple constructor.
029: */
030: public SubstanceEditorTabDisplayerUI(TabDisplayer displayer) {
031: super (displayer);
032: if (MemoryAnalyzer.isRunning()) {
033: MemoryAnalyzer
034: .enqueueUsage("SubstanceEditorTabDisplayerUI initialized");
035: }
036: }
037:
038: /* (non-Javadoc)
039: * @see org.netbeans.swing.tabcontrol.plaf.BasicTabDisplayerUI#createDefaultRenderer()
040: */
041: protected TabCellRenderer createDefaultRenderer() {
042: return new SubstanceTabCellRenderer();
043: }
044:
045: /* (non-Javadoc)
046: * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
047: */
048: public static ComponentUI createUI(JComponent c) {
049: return new SubstanceEditorTabDisplayerUI((TabDisplayer) c);
050: }
051:
052: @Override
053: protected Font createFont() {
054: return SubstanceLookAndFeel.getFontPolicy().getFontSet(
055: "Substance", null).getControlFont();
056: }
057:
058: /* (non-Javadoc)
059: * @see javax.swing.plaf.ComponentUI#getMinimumSize(javax.swing.JComponent)
060: */
061: public Dimension getMinimumSize(JComponent c) {
062: return new Dimension(80, 28);
063: }
064:
065: /* (non-Javadoc)
066: * @see javax.swing.plaf.ComponentUI#getPreferredSize(javax.swing.JComponent)
067: */
068: public Dimension getPreferredSize(JComponent c) {
069: int prefHeight = 28;
070: Graphics g = BasicScrollingTabDisplayerUI
071: .getOffscreenGraphics();
072: if (g != null) {
073: FontMetrics fm = g.getFontMetrics(displayer.getFont());
074: Insets ins = getTabAreaInsets();
075: prefHeight = fm.getHeight() + ins.top + ins.bottom + 6;
076: }
077: return new Dimension(displayer.getWidth(), prefHeight);
078: }
079:
080: /* (non-Javadoc)
081: * @see org.netbeans.swing.tabcontrol.plaf.BasicTabDisplayerUI#createRepaintPolicy()
082: */
083: protected int createRepaintPolicy() {
084: return TabState.REPAINT_ALL_TABS_ON_ACTIVATION_CHANGE
085: | TabState.REPAINT_ALL_TABS_ON_SELECTION_CHANGE
086: | TabState.REPAINT_ON_MOUSE_ENTER_CLOSE_BUTTON;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.netbeans.swing.tabcontrol.plaf.BasicTabDisplayerUI#getTabAreaInsets()
091: */
092: public Insets getTabAreaInsets() {
093: return new Insets(0, 0, 2, 70);
094: }
095:
096: @Override
097: public void update(Graphics graphics, JComponent jComponent) {
098: Graphics2D g2 = (Graphics2D) graphics.create();
099: RenderingUtils.installDesktopHints(g2);
100: // displayer.setFont(SubstanceLookAndFeel.getFontPolicy().
101: // getFontSet("Substance", null).getControlFont());
102: super .update(g2, jComponent);
103: g2.dispose();
104: }
105:
106: /* (non-Javadoc)
107: * @see org.netbeans.swing.tabcontrol.plaf.AbstractTabDisplayerUI#install()
108: */
109: public void install() {
110: super .install();
111: displayer.setBackground(UIManager.getColor("control")); // NOI18N
112: displayer.setFont(SubstanceLookAndFeel.getFontPolicy()
113: .getFontSet("Substance", null).getControlFont());
114: }
115:
116: protected void paintAfterTabs(Graphics g) {
117: Rectangle r = new Rectangle();
118: getTabsVisibleArea(r);
119: r.width = displayer.getWidth();
120: g.setColor(SubstanceLookAndFeel.getDefaultColorScheme()
121: .getMidColor());
122: g.drawLine(0, displayer.getHeight() - 1, displayer.getWidth(),
123: displayer.getHeight() - 1);
124: }
125:
126: /* (non-Javadoc)
127: * @see org.netbeans.swing.tabcontrol.plaf.BasicScrollingTabDisplayerUI#createLayout()
128: */
129: protected LayoutManager createLayout() {
130: return new SubstanceTabLayout();
131: }
132:
133: public Rectangle getTabRect(int idx, Rectangle rect) {
134: Rectangle retValue;
135:
136: retValue = super .getTabRect(idx, rect);
137: // if (selectionModel.getSelectedIndex() == idx) {
138: // retValue.height+=4;
139: // retValue.y -= 2;
140: // }
141: retValue.width += 4;
142: return retValue;
143: }
144:
145: @Override
146: protected Component getControlButtons() {
147: Component result = super .getControlButtons();
148: // this.configureButtons(result);
149: return result;
150: }
151:
152: private void configureButtons(Component comp) {
153: if (comp instanceof JButton) {
154: configureControlButton((JButton) comp);
155: }
156: if (comp instanceof Container) {
157: Container cont = (Container) comp;
158: for (int i = 0; i < cont.getComponentCount(); i++) {
159: configureButtons(cont.getComponent(i));
160: }
161: }
162: }
163:
164: private void configureControlButton(JButton button) {
165: button.setContentAreaFilled(true);
166: // button.putClientProperty(SubstanceLookAndFeel.FLAT_PROPERTY,
167: // Boolean.TRUE);
168: button.setBorderPainted(true);
169: button.setBorder(new EmptyBorder(0, 0, 0, 0));
170: }
171:
172: @Override
173: public Icon getButtonIcon(int buttonId, int buttonState) {
174: SubstanceTheme theme = SubstanceLookAndFeel.getTheme();
175: Icon result = SubstanceNetbeansImageCreator.getTabIcon(theme,
176: buttonId);
177: if (result == null) {
178: return super .getButtonIcon(buttonId, buttonState);
179: }
180: return result;
181: }
182:
183: /**
184: * Layout manager for the tab component.
185: *
186: * @author Kirill Grouchnikov
187: */
188: private class SubstanceTabLayout implements LayoutManager {
189:
190: /* (non-Javadoc)
191: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
192: */
193: public void addLayoutComponent(String name,
194: java.awt.Component comp) {
195: }
196:
197: /* (non-Javadoc)
198: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
199: */
200: public void layoutContainer(java.awt.Container parent) {
201: Insets in = getTabAreaInsets();
202: Component[] c = parent.getComponents();
203: int x = parent.getWidth() - in.right + 3;
204: int y = 0;
205: Dimension psize;
206: for (int i = 0; i < c.length; i++) {
207: psize = c[i].getPreferredSize();
208: y = in.top + 3; // hardcoded to spec
209: int w = Math.min(psize.width, parent.getWidth() - x);
210: c[i].setBounds(x, y, w, Math.min(psize.height, parent
211: .getHeight()));
212: x += psize.width;
213: if (i == 1) {
214: x += 3;
215: }
216: }
217: }
218:
219: /* (non-Javadoc)
220: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
221: */
222: public Dimension minimumLayoutSize(java.awt.Container parent) {
223: return getPreferredSize((JComponent) parent);
224: }
225:
226: /* (non-Javadoc)
227: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
228: */
229: public Dimension preferredLayoutSize(java.awt.Container parent) {
230: return getPreferredSize((JComponent) parent);
231: }
232:
233: /* (non-Javadoc)
234: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
235: */
236: public void removeLayoutComponent(java.awt.Component comp) {
237: }
238: }
239: }
|