< prev index next >

## src/com/sun/javatest/agent/Folder.java

Print this page

     126         entries.addElement(new Entry(comp, name, color, true));
     127         return super.add(name, comp);
     128     }
     129
     130     public Component add(String name, Component comp, Color color, boolean visibleTab) {
     131         entries.addElement(new Entry(comp, name, color, visibleTab));
     132         return super.add(name, comp);
     133     }
     134
     135
     136     public void showTab(String name) {
     137         showTab(name, true);
     138     }
     139
     140     public void hideTab(String name) {
     141         showTab(name, false);
     142     }
     143
     144     public void showTab(String name, boolean visible) {
     145         for (int i = 0; i < entries.size(); i++) {
     146             Entry e = (Entry)entries.elementAt(i);
     147             if (e.name.equals(name)) {
     148                 if (e.visibleTab != visible) {
     149                     e.visibleTab = visible;
     150                     repaint();
     151                 }
     152                 return;
     153             }
     154         }
     155     }
     156
     157     public String getNextVisibleTab() {
     158         int currentIndex = Math.max(0, getCurrentIndex());
     159         for (int i = currentIndex + 1; i < entries.size(); i++) {
     160             Entry e = (Entry)entries.elementAt(i);
     161             if (e.visibleTab)
     162                 return e.name;
     163         }
     164         return null;
     165     }
     166
     167     public String getPrevVisibleTab() {
     168         int currentIndex = getCurrentIndex();
     169         for (int i = currentIndex - 1; i >= 0; i--) {
     170             Entry e = (Entry)entries.elementAt(i);
     171             if (e.visibleTab)
     172                 return e.name;
     173         }
     174         return null;
     175     }
     176
     177     public Component current() {
     178         for (int i = 0; i < entries.size(); i++) {
     179             Entry e = (Entry)entries.elementAt(i);
     180             if (e.comp.isVisible())
     181                 return e.comp;
     182         }
     183         return null;
     184     }
     185
     186     public void show(Component comp) {
     187         for (int i = 0; i < entries.size(); i++) {
     188             Entry e = (Entry)entries.elementAt(i);
     189             if (e.comp == comp) {
     190                 show(e);
     191                 return;
     192             }
     193         }
     194     }
     195
     196     public void show(String name) {
     197         for (int i = 0; i < entries.size(); i++) {
     198             Entry e = (Entry)entries.elementAt(i);
     199             if (e.name.equals(name)) {
     200                 show(e);
     201                 return;
     202             }
     203         }
     204     }
     205
     206     public void paint(Graphics g) {
     207
     208         if (entries == null || entries.isEmpty()) {
     209             return;
     210         }
     211
     212         Dimension dims = getSize();
     213
     214         FontMetrics fm = g.getFontMetrics();
     215         int tabHeight = fm.getHeight() + tabpad;
     216         Entry selected = null;
     217         for (int i = 0; i < entries.size(); i++) {
     218             Entry e = (Entry)entries.elementAt(i);
     219             if (e.comp.isVisible()) {
     220                 selected = e;
     221                 break;
     222             }
     223         }
     224         if (selected != null) {
     225             g.setColor(selected.color != null ? selected.color : getBackground());
     226         }
     227         g.fillRoundRect(hgap, tabHeight + vgap,
     228             dims.width - 2*hgap - 1, dims.height - tabHeight - 2*vgap - 1,
     229             border, border);
     230         g.setColor(getForeground());
     231         g.drawRoundRect(hgap, tabHeight + vgap,
     232               dims.width - 2*hgap - 1, dims.height - tabHeight - 2*vgap - 1,
     233               border, border);
     234         int x = border + hgap;
     235         int baseLine = tabHeight + vgap;
     236         for (int i = 0; i < entries.size(); i++) {
     237             Entry e = (Entry)entries.elementAt(i);
     238             int tabH = (e.visibleTab || e.comp.isVisible() ? tabHeight : tabHeight / 3);
     239             int w = fm.stringWidth(e.name);
     240             Polygon tab = new Polygon();
     241             tab.addPoint(x, baseLine);
     242             tab.addPoint(x + slant, baseLine - tabH);
     243             tab.addPoint(x + slant + w, baseLine - tabH);
     244             tab.addPoint(x + slant + w + slant, baseLine);
     245             if (e.color != null) {
     246                 g.setColor(e.color);
     247                 g.fillPolygon(tab);
     248             }
     249             g.setColor(getForeground());
     250             g.drawLine(tab.xpoints[0], tab.ypoints[0], tab.xpoints[1], tab.ypoints[1]);
     251             g.drawLine(tab.xpoints[1], tab.ypoints[1], tab.xpoints[2], tab.ypoints[2]);
     252             g.drawLine(tab.xpoints[2], tab.ypoints[2], tab.xpoints[3], tab.ypoints[3]);
     253             if (e.visibleTab || e.comp.isVisible())
     254                 g.drawString(e.name, x + slant, baseLine - tabpad/2 - fm.getDescent());
     255             if (e.comp.isVisible()) {
     256                 g.setColor(e.color != null ? e.color : getBackground());
     257                 g.drawLine(tab.xpoints[0], tab.ypoints[0], tab.xpoints[3], tab.ypoints[3]);
     265     public void addItemListener(ItemListener l) {
     266         itemListener = AWTEventMulticaster.add(itemListener, l);
     267     }
     268
     269     public void removeItemListener(ItemListener l) {
     270         itemListener = AWTEventMulticaster.remove(itemListener, l);
     271     }
     272
     273     public Object[] getSelectedObjects() {
     274         Entry ce = getCurrentEntry();
     275         if (ce == null)
     276             return null;
     277         else
     278             return new Object[] { ce.comp };
     279     }
     280
     281     int getMinimumWidth() {
     282         FontMetrics fm = getFontMetrics(getFont());
     283         int w = border + 2 * hgap + tabSpace;
     284         for (int i = 0; i < entries.size(); i++) {
     285           Entry e = (Entry)entries.elementAt(i);
     286           w += slant + (fm == null ? 0 : fm.stringWidth(e.name)) + slant + tabSpace;
     287         }
     288         return w;
     289     }
     290
     291     private void show(Entry e) {
     292         Entry prevEntry = getCurrentEntry();
     293         Component prevComp = (prevEntry == null ? null : prevEntry.comp);
     294
     295         ((CardLayout)getLayout()).show(this, e.name);
     296         repaint(); // needed to repaint the tab area
     297
     298         if (itemListener != null) {
     299             ItemEvent ev1 =
     300                 new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
     301                               prevComp, ItemEvent.DESELECTED);
     302             ItemEvent ev2 =
     303                 new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
     304                               e.comp, ItemEvent.SELECTED);
     305             itemListener.itemStateChanged(ev1);
     306             itemListener.itemStateChanged(ev2);
     307         }
     308     }
     309
     310     private int getCurrentIndex() {
     311         for (int i = 0; i < entries.size(); i++) {
     312             Entry e = (Entry)entries.elementAt(i);
     313             if (e.comp.isVisible())
     314                 return i;
     315         }
     316         return -1;
     317     }
     318
     319     private Entry getCurrentEntry() {
     320         int i = getCurrentIndex();
     321         return (i == -1 ? null : (Entry)(entries.elementAt(i)) );
     322     }
     323
     324     void mousePressed(int mouseX, int mouseY) {
     325         FontMetrics fm = getFontMetrics(getFont());
     326         if (vgap < mouseY  &&  mouseY < border + fm.getHeight() + vgap) {
     327             int x = border + hgap;
     328             for (int i = 0; i < entries.size(); i++) {
     329                 Entry e = (Entry)entries.elementAt(i);
     330                 int w = fm.stringWidth(e.name);
     331                 x += slant + w + slant + tabSpace;
     332                 if (mouseX < x) {
     333                     show(e);
     334                     break;
     335                 }
     336             }
     337         }
     338     }
     339
     340
     341     private ItemListener itemListener;
     342     private Vector entries = new Vector();
     343     private int border;
     344     private int slant;
     345     private int tabSpace;
     346     private int hgap;
     347     private int vgap;
     348     private int tabpad;
     349 }
     350
     126         entries.addElement(new Entry(comp, name, color, true));
     127         return super.add(name, comp);
     128     }
     129
     130     public Component add(String name, Component comp, Color color, boolean visibleTab) {
     131         entries.addElement(new Entry(comp, name, color, visibleTab));
     132         return super.add(name, comp);
     133     }
     134
     135
     136     public void showTab(String name) {
     137         showTab(name, true);
     138     }
     139
     140     public void hideTab(String name) {
     141         showTab(name, false);
     142     }
     143
     144     public void showTab(String name, boolean visible) {
     145         for (int i = 0; i < entries.size(); i++) {
     146             Entry e = entries.elementAt(i);
     147             if (e.name.equals(name)) {
     148                 if (e.visibleTab != visible) {
     149                     e.visibleTab = visible;
     150                     repaint();
     151                 }
     152                 return;
     153             }
     154         }
     155     }
     156
     157     public String getNextVisibleTab() {
     158         int currentIndex = Math.max(0, getCurrentIndex());
     159         for (int i = currentIndex + 1; i < entries.size(); i++) {
     160             Entry e = entries.elementAt(i);
     161             if (e.visibleTab)
     162                 return e.name;
     163         }
     164         return null;
     165     }
     166
     167     public String getPrevVisibleTab() {
     168         int currentIndex = getCurrentIndex();
     169         for (int i = currentIndex - 1; i >= 0; i--) {
     170             Entry e = entries.elementAt(i);
     171             if (e.visibleTab)
     172                 return e.name;
     173         }
     174         return null;
     175     }
     176
     177     public Component current() {
     178         for (int i = 0; i < entries.size(); i++) {
     179             Entry e = entries.elementAt(i);
     180             if (e.comp.isVisible())
     181                 return e.comp;
     182         }
     183         return null;
     184     }
     185
     186     public void show(Component comp) {
     187         for (int i = 0; i < entries.size(); i++) {
     188             Entry e = entries.elementAt(i);
     189             if (e.comp == comp) {
     190                 show(e);
     191                 return;
     192             }
     193         }
     194     }
     195
     196     public void show(String name) {
     197         for (int i = 0; i < entries.size(); i++) {
     198             Entry e = entries.elementAt(i);
     199             if (e.name.equals(name)) {
     200                 show(e);
     201                 return;
     202             }
     203         }
     204     }
     205
     206     public void paint(Graphics g) {
     207
     208         if (entries == null || entries.isEmpty()) {
     209             return;
     210         }
     211
     212         Dimension dims = getSize();
     213
     214         FontMetrics fm = g.getFontMetrics();
     215         int tabHeight = fm.getHeight() + tabpad;
     216         Entry selected = null;
     217         for (int i = 0; i < entries.size(); i++) {
     218             Entry e = entries.elementAt(i);
     219             if (e.comp.isVisible()) {
     220                 selected = e;
     221                 break;
     222             }
     223         }
     224         if (selected != null) {
     225             g.setColor(selected.color != null ? selected.color : getBackground());
     226         }
     227         g.fillRoundRect(hgap, tabHeight + vgap,
     228             dims.width - 2*hgap - 1, dims.height - tabHeight - 2*vgap - 1,
     229             border, border);
     230         g.setColor(getForeground());
     231         g.drawRoundRect(hgap, tabHeight + vgap,
     232               dims.width - 2*hgap - 1, dims.height - tabHeight - 2*vgap - 1,
     233               border, border);
     234         int x = border + hgap;
     235         int baseLine = tabHeight + vgap;
     236         for (int i = 0; i < entries.size(); i++) {
     237             Entry e = entries.elementAt(i);
     238             int tabH = (e.visibleTab || e.comp.isVisible() ? tabHeight : tabHeight / 3);
     239             int w = fm.stringWidth(e.name);
     240             Polygon tab = new Polygon();
     241             tab.addPoint(x, baseLine);
     242             tab.addPoint(x + slant, baseLine - tabH);
     243             tab.addPoint(x + slant + w, baseLine - tabH);
     244             tab.addPoint(x + slant + w + slant, baseLine);
     245             if (e.color != null) {
     246                 g.setColor(e.color);
     247                 g.fillPolygon(tab);
     248             }
     249             g.setColor(getForeground());
     250             g.drawLine(tab.xpoints[0], tab.ypoints[0], tab.xpoints[1], tab.ypoints[1]);
     251             g.drawLine(tab.xpoints[1], tab.ypoints[1], tab.xpoints[2], tab.ypoints[2]);
     252             g.drawLine(tab.xpoints[2], tab.ypoints[2], tab.xpoints[3], tab.ypoints[3]);
     253             if (e.visibleTab || e.comp.isVisible())
     254                 g.drawString(e.name, x + slant, baseLine - tabpad/2 - fm.getDescent());
     255             if (e.comp.isVisible()) {
     256                 g.setColor(e.color != null ? e.color : getBackground());
     257                 g.drawLine(tab.xpoints[0], tab.ypoints[0], tab.xpoints[3], tab.ypoints[3]);
     265     public void addItemListener(ItemListener l) {
     266         itemListener = AWTEventMulticaster.add(itemListener, l);
     267     }
     268
     269     public void removeItemListener(ItemListener l) {
     270         itemListener = AWTEventMulticaster.remove(itemListener, l);
     271     }
     272
     273     public Object[] getSelectedObjects() {
     274         Entry ce = getCurrentEntry();
     275         if (ce == null)
     276             return null;
     277         else
     278             return new Object[] { ce.comp };
     279     }
     280
     281     int getMinimumWidth() {
     282         FontMetrics fm = getFontMetrics(getFont());
     283         int w = border + 2 * hgap + tabSpace;
     284         for (int i = 0; i < entries.size(); i++) {
     285           Entry e = entries.elementAt(i);
     286           w += slant + (fm == null ? 0 : fm.stringWidth(e.name)) + slant + tabSpace;
     287         }
     288         return w;
     289     }
     290
     291     private void show(Entry e) {
     292         Entry prevEntry = getCurrentEntry();
     293         Component prevComp = (prevEntry == null ? null : prevEntry.comp);
     294
     295         ((CardLayout)getLayout()).show(this, e.name);
     296         repaint(); // needed to repaint the tab area
     297
     298         if (itemListener != null) {
     299             ItemEvent ev1 =
     300                 new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
     301                               prevComp, ItemEvent.DESELECTED);
     302             ItemEvent ev2 =
     303                 new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
     304                               e.comp, ItemEvent.SELECTED);
     305             itemListener.itemStateChanged(ev1);
     306             itemListener.itemStateChanged(ev2);
     307         }
     308     }
     309
     310     private int getCurrentIndex() {
     311         for (int i = 0; i < entries.size(); i++) {
     312             Entry e = entries.elementAt(i);
     313             if (e.comp.isVisible())
     314                 return i;
     315         }
     316         return -1;
     317     }
     318
     319     private Entry getCurrentEntry() {
     320         int i = getCurrentIndex();
     321         return (i == -1 ? null : entries.elementAt(i));
     322     }
     323
     324     void mousePressed(int mouseX, int mouseY) {
     325         FontMetrics fm = getFontMetrics(getFont());
     326         if (vgap < mouseY  &&  mouseY < border + fm.getHeight() + vgap) {
     327             int x = border + hgap;
     328             for (int i = 0; i < entries.size(); i++) {
     329                 Entry e = entries.elementAt(i);
     330                 int w = fm.stringWidth(e.name);
     331                 x += slant + w + slant + tabSpace;
     332                 if (mouseX < x) {
     333                     show(e);
     334                     break;
     335                 }
     336             }
     337         }
     338     }
     339
     340
     341     private ItemListener itemListener;
     342     private Vector<Entry> entries = new Vector<>();
     343     private int border;
     344     private int slant;
     345     private int tabSpace;
     346     private int hgap;
     347     private int vgap;
     348     private int tabpad;
     349 }
     350

< prev index next >