1 30 31 package com.jgoodies.uif_lite.panel; 32 33 import java.awt.*; 34 35 import javax.swing.*; 36 import javax.swing.border.AbstractBorder ; 37 38 import com.jgoodies.looks.LookUtils; 39 40 65 66 public class SimpleInternalFrame extends JPanel { 67 68 private JLabel titleLabel; 69 private GradientPanel gradientPanel; 70 private JPanel headerPanel; 71 private boolean selected; 72 73 74 76 83 public SimpleInternalFrame(String title) { 84 this(null, title, null, null); 85 } 86 87 88 95 public SimpleInternalFrame(Icon icon, String title) { 96 this(icon, title, null, null); 97 } 98 99 100 108 public SimpleInternalFrame(String title, JToolBar bar, JComponent content) { 109 this(null, title, bar, content); 110 } 111 112 113 122 public SimpleInternalFrame( 123 Icon icon, 124 String title, 125 JToolBar bar, 126 JComponent content) { 127 super(new BorderLayout()); 128 this.selected = false; 129 this.titleLabel = new JLabel(title, icon, SwingConstants.LEADING); 130 JPanel top = buildHeader(titleLabel, bar); 131 132 add(top, BorderLayout.NORTH); 133 if (content != null) { 134 setContent(content); 135 } 136 setBorder(new ShadowBorder()); 137 setSelected(true); 138 updateHeader(); 139 } 140 141 142 144 149 public Icon getFrameIcon() { 150 return titleLabel.getIcon(); 151 } 152 153 154 159 public void setFrameIcon(Icon newIcon) { 160 Icon oldIcon = getFrameIcon(); 161 titleLabel.setIcon(newIcon); 162 firePropertyChange("frameIcon", oldIcon, newIcon); 163 } 164 165 166 171 public String getTitle() { 172 return titleLabel.getText(); 173 } 174 175 176 181 public void setTitle(String newText) { 182 String oldText = getTitle(); 183 titleLabel.setText(newText); 184 firePropertyChange("title", oldText, newText); 185 } 186 187 188 193 public JToolBar getToolBar() { 194 return headerPanel.getComponentCount() > 1 195 ? (JToolBar) headerPanel.getComponent(1) 196 : null; 197 } 198 199 200 205 public void setToolBar(JToolBar newToolBar) { 206 JToolBar oldToolBar = getToolBar(); 207 if (oldToolBar == newToolBar) { 208 return; 209 } 210 if (oldToolBar != null) { 211 headerPanel.remove(oldToolBar); 212 } 213 if (newToolBar != null) { 214 newToolBar.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 215 headerPanel.add(newToolBar, BorderLayout.EAST); 216 } 217 updateHeader(); 218 firePropertyChange("toolBar", oldToolBar, newToolBar); 219 } 220 221 222 227 public Component getContent() { 228 return hasContent() ? getComponent(1) : null; 229 } 230 231 232 237 public void setContent(Component newContent) { 238 Component oldContent = getContent(); 239 if (hasContent()) { 240 remove(oldContent); 241 } 242 add(newContent, BorderLayout.CENTER); 243 firePropertyChange("content", oldContent, newContent); 244 } 245 246 247 255 public boolean isSelected() { 256 return selected; 257 } 258 259 260 269 public void setSelected(boolean newValue) { 270 boolean oldValue = isSelected(); 271 selected = newValue; 272 updateHeader(); 273 firePropertyChange("selected", oldValue, newValue); 274 } 275 276 277 279 287 private JPanel buildHeader(JLabel label, JToolBar bar) { 288 gradientPanel = 289 new GradientPanel(new BorderLayout(), getHeaderBackground()); 290 label.setOpaque(false); 291 292 gradientPanel.add(label, BorderLayout.WEST); 293 gradientPanel.setBorder(BorderFactory.createEmptyBorder(3, 4, 3, 1)); 294 295 headerPanel = new JPanel(new BorderLayout()); 296 headerPanel.add(gradientPanel, BorderLayout.CENTER); 297 setToolBar(bar); 298 headerPanel.setBorder(new RaisedHeaderBorder()); 299 headerPanel.setOpaque(false); 300 return headerPanel; 301 } 302 303 306 private void updateHeader() { 307 gradientPanel.setBackground(getHeaderBackground()); 308 gradientPanel.setOpaque(isSelected()); 309 titleLabel.setForeground(getTextForeground(isSelected())); 310 headerPanel.repaint(); 311 } 312 313 314 318 public void updateUI() { 319 super.updateUI(); 320 if (titleLabel != null) { 321 updateHeader(); 322 } 323 } 324 325 326 328 333 private boolean hasContent() { 334 return getComponentCount() > 1; 335 } 336 337 345 protected Color getTextForeground(boolean isSelected) { 346 Color c = 347 UIManager.getColor( 348 isSelected 349 ? "SimpleInternalFrame.activeTitleForeground" 350 : "SimpleInternalFrame.inactiveTitleForeground"); 351 if (c != null) { 352 return c; 353 } 354 return UIManager.getColor( 355 isSelected 356 ? "InternalFrame.activeTitleForeground" 357 : "Label.foreground"); 358 359 } 360 361 368 protected Color getHeaderBackground() { 369 Color c = 370 UIManager.getColor("SimpleInternalFrame.activeTitleBackground"); 371 if (c != null) 372 return c; 373 if (LookUtils.IS_LAF_WINDOWS_XP_ENABLED) 374 c = UIManager.getColor("InternalFrame.activeTitleGradient"); 375 return c != null 376 ? c 377 : UIManager.getColor("InternalFrame.activeTitleBackground"); 378 } 379 380 381 383 private static class RaisedHeaderBorder extends AbstractBorder { 385 386 private static final Insets INSETS = new Insets(1, 1, 1, 0); 387 388 public Insets getBorderInsets(Component c) { return INSETS; } 389 390 public void paintBorder(Component c, Graphics g, 391 int x, int y, int w, int h) { 392 393 g.translate(x, y); 394 g.setColor(UIManager.getColor("controlLtHighlight")); 395 g.fillRect(0, 0, w, 1); 396 g.fillRect(0, 1, 1, h-1); 397 g.setColor(UIManager.getColor("controlShadow")); 398 g.fillRect(0, h-1, w, 1); 399 g.translate(-x, -y); 400 } 401 } 402 403 private static class ShadowBorder extends AbstractBorder { 405 406 private static final Insets INSETS = new Insets(1, 1, 3, 3); 407 408 public Insets getBorderInsets(Component c) { return INSETS; } 409 410 public void paintBorder(Component c, Graphics g, 411 int x, int y, int w, int h) { 412 413 Color shadow = UIManager.getColor("controlShadow"); 414 if (shadow == null) { 415 shadow = Color.GRAY; 416 } 417 Color lightShadow = new Color(shadow.getRed(), 418 shadow.getGreen(), 419 shadow.getBlue(), 420 170); 421 Color lighterShadow = new Color(shadow.getRed(), 422 shadow.getGreen(), 423 shadow.getBlue(), 424 70); 425 g.translate(x, y); 426 427 g.setColor(shadow); 428 g.fillRect(0, 0, w - 3, 1); 429 g.fillRect(0, 0, 1, h - 3); 430 g.fillRect(w - 3, 1, 1, h - 3); 431 g.fillRect(1, h - 3, w - 3, 1); 432 g.setColor(lightShadow); 434 g.fillRect(w - 3, 0, 1, 1); 435 g.fillRect(0, h - 3, 1, 1); 436 g.fillRect(w - 2, 1, 1, h - 3); 437 g.fillRect(1, h - 2, w - 3, 1); 438 g.setColor(lighterShadow); 440 g.fillRect(w - 2, 0, 1, 1); 441 g.fillRect(0, h - 2, 1, 1); 442 g.fillRect(w-2, h-2, 1, 1); 443 g.fillRect(w - 1, 1, 1, h - 2); 444 g.fillRect(1, h - 1, w - 2, 1); 445 g.translate(-x, -y); 446 } 447 } 448 449 private static class GradientPanel extends JPanel { 451 452 private GradientPanel(LayoutManager lm, Color background) { 453 super(lm); 454 setBackground(background); 455 } 456 457 public void paintComponent(Graphics g) { 458 super.paintComponent(g); 459 if (!isOpaque()) { 460 return; 461 } 462 Color control = UIManager.getColor("control"); 463 int width = getWidth(); 464 int height = getHeight(); 465 466 Graphics2D g2 = (Graphics2D) g; 467 Paint storedPaint = g2.getPaint(); 468 g2.setPaint( 469 new GradientPaint(0, 0, getBackground(), width, 0, control)); 470 g2.fillRect(0, 0, width, height); 471 g2.setPaint(storedPaint); 472 } 473 } 474 475 } | Popular Tags |