| .. | .. |
|---|
| 8 | 8 | import java.util.*; |
|---|
| 9 | 9 | |
|---|
| 10 | 10 | // custom JTabbedPane-like thing. |
|---|
| 11 | | -public class LinkTabPane extends JPanel { |
|---|
| 12 | | - |
|---|
| 13 | | - ArrayList<String> tabNames=new ArrayList<String>(); |
|---|
| 14 | | - HashMap<String, JComponent> tabMap=new HashMap<String, JComponent>(); |
|---|
| 15 | | - String currentName; |
|---|
| 16 | | - CardLayout cards=new CardLayout(); |
|---|
| 17 | | - JPanel center=new JPanel(); |
|---|
| 18 | | - LinkTop top=new LinkTop(); |
|---|
| 19 | | - |
|---|
| 20 | | - public LinkTabPane() |
|---|
| 21 | | - { |
|---|
| 22 | | - setBackground(Color.white); |
|---|
| 23 | | - setLayout(new BorderLayout()); |
|---|
| 24 | | - add(top, BorderLayout.NORTH); |
|---|
| 25 | | - add(center, BorderLayout.CENTER); |
|---|
| 26 | | - center.setLayout(cards); |
|---|
| 27 | | - top.addMouseListener(new MouseAdapter() { |
|---|
| 28 | | - @Override |
|---|
| 29 | | - public void mousePressed(MouseEvent e) { |
|---|
| 30 | | - String s=top.getName(e.getX()); |
|---|
| 31 | | - if (s!=null) |
|---|
| 32 | | - { |
|---|
| 33 | | - String old=currentName; |
|---|
| 34 | | - setCurrentName(s); |
|---|
| 35 | | - firePropertyChange("tab", old, s); |
|---|
| 36 | | - } |
|---|
| 37 | | - }}); |
|---|
| 38 | | - } |
|---|
| 39 | | - |
|---|
| 40 | | - public String getTitleAt(int i) |
|---|
| 41 | | - { |
|---|
| 42 | | - return tabNames.get(i); |
|---|
| 43 | | - } |
|---|
| 44 | | - |
|---|
| 45 | | - public void setSelectedIndex(int i) |
|---|
| 46 | | - { |
|---|
| 47 | | - setCurrentName(getTitleAt(i)); |
|---|
| 48 | | - } |
|---|
| 49 | | - |
|---|
| 50 | | - public void addTab(JComponent component, String name, boolean left) |
|---|
| 51 | | - { |
|---|
| 52 | | - tabNames.add(name); |
|---|
| 53 | | - tabMap.put(name, component); |
|---|
| 54 | | - center.add(component, name); |
|---|
| 55 | | - top.addName(name, left); |
|---|
| 56 | | - repaint(); |
|---|
| 57 | | - if (currentName==null) |
|---|
| 58 | | - currentName=name; |
|---|
| 59 | | - } |
|---|
| 60 | | - |
|---|
| 61 | | - public String getCurrentName() |
|---|
| 62 | | - { |
|---|
| 63 | | - return currentName; |
|---|
| 64 | | - } |
|---|
| 65 | | - |
|---|
| 66 | | - public void setCurrentName(final String currentName) |
|---|
| 67 | | - { |
|---|
| 68 | | - this.currentName=currentName; |
|---|
| 69 | | - top.repaint(); |
|---|
| 70 | | - SwingUtilities.invokeLater(new Runnable() {public void run() {cards.show(center, currentName);}}); |
|---|
| 11 | +public class LinkTabPane extends JPanel |
|---|
| 12 | +{ |
|---|
| 71 | 13 | |
|---|
| 72 | | - } |
|---|
| 73 | | - |
|---|
| 74 | | - class LinkTop extends JPanel |
|---|
| 75 | | - { |
|---|
| 76 | | - int left, right; |
|---|
| 77 | | - ArrayList<HotLink> leftHots=new ArrayList<HotLink>(); |
|---|
| 78 | | - ArrayList<HotLink> rightHots=new ArrayList<HotLink>(); |
|---|
| 79 | | - Font font=new Font("Verdana", Font.PLAIN, 14); |
|---|
| 80 | | - FontMetrics fm=getFontMetrics(font); |
|---|
| 81 | | - |
|---|
| 82 | | - LinkTop() |
|---|
| 83 | | - { |
|---|
| 84 | | - setLayout(new FlowLayout(FlowLayout.LEFT)); |
|---|
| 85 | | - setBackground(new Color(220,220,220)); |
|---|
| 86 | | - } |
|---|
| 87 | | - |
|---|
| 88 | | - public void paintComponent(Graphics g1) |
|---|
| 89 | | - { |
|---|
| 90 | | - int w=getSize().width, h=getSize().height; |
|---|
| 91 | | - Graphics2D g=(Graphics2D)g1; |
|---|
| 92 | | - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|---|
| 93 | | - g.setColor(getBackground()); |
|---|
| 94 | | - g.fillRect(0,0,w,h); |
|---|
| 95 | | - g.setColor(Color.gray); |
|---|
| 96 | | - for (int i=0; i<2; i++) |
|---|
| 97 | | - { |
|---|
| 98 | | - g.drawLine(0,i,w,i); |
|---|
| 99 | | - g.drawLine(0,h-1-i,w,h-1-i); |
|---|
| 100 | | - } |
|---|
| 101 | | - |
|---|
| 102 | | - for (HotLink hot: leftHots) |
|---|
| 103 | | - { |
|---|
| 104 | | - draw(g, hot, h, 0); |
|---|
| 105 | | - } |
|---|
| 106 | | - |
|---|
| 107 | | - for (HotLink hot: rightHots) |
|---|
| 108 | | - { |
|---|
| 109 | | - draw(g, hot, h, w); |
|---|
| 110 | | - } |
|---|
| 111 | | - |
|---|
| 112 | | - for (int i=0; i<leftHots.size(); i++) |
|---|
| 113 | | - { |
|---|
| 114 | | - |
|---|
| 115 | | - if (i<leftHots.size()-1) |
|---|
| 116 | | - { |
|---|
| 117 | | - HotLink hot=leftHots.get(i); |
|---|
| 118 | | - for (int j=0; j<1; j++) |
|---|
| 119 | | - g.drawLine(hot.x+hot.width-1-j, 7, hot.x+hot.width-1-j, h-7); |
|---|
| 120 | | - } |
|---|
| 121 | | - } |
|---|
| 122 | | - |
|---|
| 123 | | - for (int i=0; i<rightHots.size(); i++) |
|---|
| 124 | | - { |
|---|
| 125 | | - |
|---|
| 126 | | - if (i<rightHots.size()-1) |
|---|
| 127 | | - { |
|---|
| 128 | | - HotLink hot=rightHots.get(i); |
|---|
| 129 | | - for (int j=0; j<1; j++) |
|---|
| 130 | | - g.drawLine(hot.x+w-1-j, 7, hot.x+w-1-j, h-7); |
|---|
| 131 | | - } |
|---|
| 132 | | - } |
|---|
| 133 | | - } |
|---|
| 134 | | - |
|---|
| 135 | | - void draw(Graphics g, HotLink hot, int h, int dx) |
|---|
| 136 | | - { |
|---|
| 137 | | - int x=hot.x+dx; |
|---|
| 138 | | - if (hot.s.equals(currentName)) |
|---|
| 139 | | - { |
|---|
| 140 | | - g.setColor(Color.lightGray); |
|---|
| 141 | | - g.fillRect(x,2,hot.width,h-4); |
|---|
| 142 | | - g.setColor(Color.gray); |
|---|
| 143 | | - g.drawLine(x-1, 0, x-1, h); |
|---|
| 144 | | - g.drawLine(x+hot.width-1, 0, x+hot.width-1, h); |
|---|
| 145 | | - } |
|---|
| 146 | | - g.setColor(Color.darkGray); |
|---|
| 147 | | - g.setFont(font); |
|---|
| 148 | | - int sw=fm.stringWidth(hot.s); |
|---|
| 149 | | - g.drawString(hot.s, x+(hot.width-sw)/2, h-10); |
|---|
| 150 | | - |
|---|
| 151 | | - } |
|---|
| 152 | | - |
|---|
| 153 | | - String getName(int x) |
|---|
| 154 | | - { |
|---|
| 155 | | - for (HotLink h: leftHots) |
|---|
| 156 | | - { |
|---|
| 157 | | - if (h.x<=x && h.x+h.width>x) |
|---|
| 158 | | - return h.s; |
|---|
| 159 | | - } |
|---|
| 160 | | - for (HotLink h: rightHots) |
|---|
| 161 | | - { |
|---|
| 162 | | - int w=getSize().width; |
|---|
| 163 | | - if (h.x+w<=x && h.x+h.width+w>x) |
|---|
| 164 | | - return h.s; |
|---|
| 165 | | - } |
|---|
| 14 | + ArrayList<String> tabNames = new ArrayList<String>(); |
|---|
| 15 | + HashMap<String, JComponent> tabMap = new HashMap<String, JComponent>(); |
|---|
| 16 | + String currentName; |
|---|
| 17 | + CardLayout cards = new CardLayout(); |
|---|
| 18 | + JPanel center = new JPanel(); |
|---|
| 19 | + LinkTop top = new LinkTop(); |
|---|
| 166 | 20 | |
|---|
| 167 | | - if (leftHots.size()>0) |
|---|
| 168 | | - return leftHots.get(leftHots.size()-1).s; |
|---|
| 169 | | - if (rightHots.size()>0) |
|---|
| 170 | | - return rightHots.get(0).s; |
|---|
| 171 | | - return null; |
|---|
| 172 | | - } |
|---|
| 173 | | - |
|---|
| 174 | | - void addName(String name, boolean leftward) |
|---|
| 175 | | - { |
|---|
| 176 | | - if (leftward) |
|---|
| 177 | | - { |
|---|
| 178 | | - int x=right; |
|---|
| 179 | | - int w=fm.stringWidth(name)+24; |
|---|
| 180 | | - leftHots.add(new HotLink(name, x, 0, w, 30)); |
|---|
| 181 | | - right+=w; |
|---|
| 182 | | - } |
|---|
| 183 | | - else |
|---|
| 184 | | - { |
|---|
| 185 | | - int x=left; |
|---|
| 186 | | - int w=fm.stringWidth(name)+24; |
|---|
| 187 | | - rightHots.add(new HotLink(name, x-w, 0, w, 30)); |
|---|
| 188 | | - left-=w; |
|---|
| 189 | | - } |
|---|
| 190 | | - } |
|---|
| 191 | | - |
|---|
| 192 | | - class HotLink extends Rectangle |
|---|
| 193 | | - { |
|---|
| 194 | | - String s; |
|---|
| 195 | | - HotLink(String s, int x, int y, int w, int h) |
|---|
| 196 | | - { |
|---|
| 197 | | - super(x,y,w,h); |
|---|
| 198 | | - this.s=s; |
|---|
| 199 | | - } |
|---|
| 200 | | - } |
|---|
| 201 | | - |
|---|
| 202 | | - public Dimension getPreferredSize() |
|---|
| 203 | | - { |
|---|
| 204 | | - return new Dimension(30,30); |
|---|
| 205 | | - } |
|---|
| 206 | | - } |
|---|
| 207 | | - |
|---|
| 21 | + public LinkTabPane() |
|---|
| 22 | + { |
|---|
| 23 | + setBackground(Color.white); |
|---|
| 24 | + setLayout(new BorderLayout()); |
|---|
| 25 | + add(top, BorderLayout.NORTH); |
|---|
| 26 | + add(center, BorderLayout.CENTER); |
|---|
| 27 | + center.setLayout(cards); |
|---|
| 28 | + top.addMouseListener(new MouseAdapter() |
|---|
| 29 | + { |
|---|
| 30 | + |
|---|
| 31 | + @Override |
|---|
| 32 | + public void mousePressed(MouseEvent e) |
|---|
| 33 | + { |
|---|
| 34 | + String s = top.getName(e.getX()); |
|---|
| 35 | + if (s != null) |
|---|
| 36 | + { |
|---|
| 37 | + String old = currentName; |
|---|
| 38 | + setCurrentName(s); |
|---|
| 39 | + firePropertyChange("tab", old, s); |
|---|
| 40 | + } |
|---|
| 41 | + } |
|---|
| 42 | + }); |
|---|
| 43 | + } |
|---|
| 44 | + |
|---|
| 45 | + public String getTitleAt(int i) |
|---|
| 46 | + { |
|---|
| 47 | + return tabNames.get(i); |
|---|
| 48 | + } |
|---|
| 49 | + |
|---|
| 50 | + public void setSelectedIndex(int i) |
|---|
| 51 | + { |
|---|
| 52 | + setCurrentName(getTitleAt(i)); |
|---|
| 53 | + } |
|---|
| 54 | + |
|---|
| 55 | + public void addTab(JComponent component, String name, boolean left) |
|---|
| 56 | + { |
|---|
| 57 | + tabNames.add(name); |
|---|
| 58 | + tabMap.put(name, component); |
|---|
| 59 | + center.add(component, name); |
|---|
| 60 | + top.addName(name, left); |
|---|
| 61 | + repaint(); |
|---|
| 62 | + if (currentName == null) |
|---|
| 63 | + { |
|---|
| 64 | + currentName = name; |
|---|
| 65 | + } |
|---|
| 66 | + } |
|---|
| 67 | + |
|---|
| 68 | + public String getCurrentName() |
|---|
| 69 | + { |
|---|
| 70 | + return currentName; |
|---|
| 71 | + } |
|---|
| 72 | + |
|---|
| 73 | + public void setCurrentName(final String currentName) |
|---|
| 74 | + { |
|---|
| 75 | + this.currentName = currentName; |
|---|
| 76 | + top.repaint(); |
|---|
| 77 | + SwingUtilities.invokeLater(new Runnable() |
|---|
| 78 | + { |
|---|
| 79 | + public void run() |
|---|
| 80 | + { |
|---|
| 81 | + cards.show(center, currentName); |
|---|
| 82 | + } |
|---|
| 83 | + }); |
|---|
| 84 | + |
|---|
| 85 | + } |
|---|
| 86 | + |
|---|
| 87 | + class LinkTop extends JPanel |
|---|
| 88 | + { |
|---|
| 89 | + int left, right; |
|---|
| 90 | + ArrayList<HotLink> leftHots = new ArrayList<HotLink>(); |
|---|
| 91 | + ArrayList<HotLink> rightHots = new ArrayList<HotLink>(); |
|---|
| 92 | + Font font = new Font("Verdana", Font.PLAIN, 14); |
|---|
| 93 | + FontMetrics fm = getFontMetrics(font); |
|---|
| 94 | + |
|---|
| 95 | + LinkTop() |
|---|
| 96 | + { |
|---|
| 97 | + setLayout(new FlowLayout(FlowLayout.LEFT)); |
|---|
| 98 | + setBackground(new Color(220, 220, 220)); |
|---|
| 99 | + } |
|---|
| 100 | + |
|---|
| 101 | + public void paintComponent(Graphics g1) |
|---|
| 102 | + { |
|---|
| 103 | + int w = getSize().width, h = getSize().height; |
|---|
| 104 | + Graphics2D g = (Graphics2D) g1; |
|---|
| 105 | + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|---|
| 106 | + g.setColor(getBackground()); |
|---|
| 107 | + g.fillRect(0, 0, w, h); |
|---|
| 108 | + g.setColor(Color.gray); |
|---|
| 109 | + for (int i = 0; i < 2; i++) |
|---|
| 110 | + { |
|---|
| 111 | + g.drawLine(0, i, w, i); |
|---|
| 112 | + g.drawLine(0, h - 1 - i, w, h - 1 - i); |
|---|
| 113 | + } |
|---|
| 114 | + |
|---|
| 115 | + for (HotLink hot : leftHots) |
|---|
| 116 | + { |
|---|
| 117 | + draw(g, hot, h, 0); |
|---|
| 118 | + } |
|---|
| 119 | + |
|---|
| 120 | + for (HotLink hot : rightHots) |
|---|
| 121 | + { |
|---|
| 122 | + draw(g, hot, h, w); |
|---|
| 123 | + } |
|---|
| 124 | + |
|---|
| 125 | + for (int i = 0; i < leftHots.size(); i++) |
|---|
| 126 | + { |
|---|
| 127 | + |
|---|
| 128 | + if (i < leftHots.size() - 1) |
|---|
| 129 | + { |
|---|
| 130 | + HotLink hot = leftHots.get(i); |
|---|
| 131 | + for (int j = 0; j < 1; j++) |
|---|
| 132 | + { |
|---|
| 133 | + g.drawLine(hot.x + hot.width - 1 - j, 7, hot.x + hot.width - 1 - j, h - 7); |
|---|
| 134 | + } |
|---|
| 135 | + } |
|---|
| 136 | + } |
|---|
| 137 | + |
|---|
| 138 | + for (int i = 0; i < rightHots.size(); i++) |
|---|
| 139 | + { |
|---|
| 140 | + |
|---|
| 141 | + if (i < rightHots.size() - 1) |
|---|
| 142 | + { |
|---|
| 143 | + HotLink hot = rightHots.get(i); |
|---|
| 144 | + for (int j = 0; j < 1; j++) |
|---|
| 145 | + { |
|---|
| 146 | + g.drawLine(hot.x + w - 1 - j, 7, hot.x + w - 1 - j, h - 7); |
|---|
| 147 | + } |
|---|
| 148 | + } |
|---|
| 149 | + } |
|---|
| 150 | + } |
|---|
| 151 | + |
|---|
| 152 | + void draw(Graphics g, HotLink hot, int h, int dx) |
|---|
| 153 | + { |
|---|
| 154 | + int x = hot.x + dx; |
|---|
| 155 | + if (hot.s.equals(currentName)) |
|---|
| 156 | + { |
|---|
| 157 | + g.setColor(Color.lightGray); |
|---|
| 158 | + g.fillRect(x, 2, hot.width, h - 4); |
|---|
| 159 | + g.setColor(Color.gray); |
|---|
| 160 | + g.drawLine(x - 1, 0, x - 1, h); |
|---|
| 161 | + g.drawLine(x + hot.width - 1, 0, x + hot.width - 1, h); |
|---|
| 162 | + } |
|---|
| 163 | + g.setColor(Color.darkGray); |
|---|
| 164 | + g.setFont(font); |
|---|
| 165 | + int sw = fm.stringWidth(hot.s); |
|---|
| 166 | + g.drawString(hot.s, x + (hot.width - sw) / 2, h - 10); |
|---|
| 167 | + |
|---|
| 168 | + } |
|---|
| 169 | + |
|---|
| 170 | + String getName(int x) |
|---|
| 171 | + { |
|---|
| 172 | + for (HotLink h : leftHots) |
|---|
| 173 | + { |
|---|
| 174 | + if (h.x <= x && h.x + h.width > x) |
|---|
| 175 | + { |
|---|
| 176 | + return h.s; |
|---|
| 177 | + } |
|---|
| 178 | + } |
|---|
| 179 | + for (HotLink h : rightHots) |
|---|
| 180 | + { |
|---|
| 181 | + int w = getSize().width; |
|---|
| 182 | + if (h.x + w <= x && h.x + h.width + w > x) |
|---|
| 183 | + { |
|---|
| 184 | + return h.s; |
|---|
| 185 | + } |
|---|
| 186 | + } |
|---|
| 187 | + |
|---|
| 188 | + if (leftHots.size() > 0) |
|---|
| 189 | + { |
|---|
| 190 | + return leftHots.get(leftHots.size() - 1).s; |
|---|
| 191 | + } |
|---|
| 192 | + if (rightHots.size() > 0) |
|---|
| 193 | + { |
|---|
| 194 | + return rightHots.get(0).s; |
|---|
| 195 | + } |
|---|
| 196 | + return null; |
|---|
| 197 | + } |
|---|
| 198 | + |
|---|
| 199 | + void addName(String name, boolean leftward) |
|---|
| 200 | + { |
|---|
| 201 | + if (leftward) |
|---|
| 202 | + { |
|---|
| 203 | + int x = right; |
|---|
| 204 | + int w = fm.stringWidth(name) + 24; |
|---|
| 205 | + leftHots.add(new HotLink(name, x, 0, w, 30)); |
|---|
| 206 | + right += w; |
|---|
| 207 | + } else |
|---|
| 208 | + { |
|---|
| 209 | + int x = left; |
|---|
| 210 | + int w = fm.stringWidth(name) + 24; |
|---|
| 211 | + rightHots.add(new HotLink(name, x - w, 0, w, 30)); |
|---|
| 212 | + left -= w; |
|---|
| 213 | + } |
|---|
| 214 | + } |
|---|
| 215 | + |
|---|
| 216 | + class HotLink extends Rectangle |
|---|
| 217 | + { |
|---|
| 218 | + String s; |
|---|
| 219 | + |
|---|
| 220 | + HotLink(String s, int x, int y, int w, int h) |
|---|
| 221 | + { |
|---|
| 222 | + super(x, y, w, h); |
|---|
| 223 | + this.s = s; |
|---|
| 224 | + } |
|---|
| 225 | + } |
|---|
| 226 | + |
|---|
| 227 | + public Dimension getPreferredSize() |
|---|
| 228 | + { |
|---|
| 229 | + return new Dimension(30, 30); |
|---|
| 230 | + } |
|---|
| 231 | + } |
|---|
| 208 | 232 | } |
|---|