Java/Swing Components/Dockable
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Add border for dockable component
package net.eleritec.docking.demos.border;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.ruponentProviderAdapter;
import net.eleritec.docking.defaults.DefaultDockingPort;
public class BorderDemo extends JFrame {
public BorderDemo() {
super("Border Docking Demo");
setContentPane(createContentPane());
}
private JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort("North"), BorderLayout.NORTH);
p.add(buildDockingPort("South"), BorderLayout.SOUTH);
p.add(buildDockingPort("East"), BorderLayout.EAST);
p.add(buildDockingPort("West"), BorderLayout.WEST);
p.add(createDockingPort(), BorderLayout.CENTER);
return p;
}
private DefaultDockingPort buildDockingPort(String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort();
// create the Dockable panel
DockablePanel panel = new DockablePanel(desc);
// dock the panel and return the DockingPort
port.dock(panel.getDockable(), DockingPort.CENTER_REGION);
return port;
}
private DefaultDockingPort createDockingPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setPreferredSize(new Dimension(100, 100));
port.setBorderManager(new DemoBorderManager());
port.setComponentProvider(new ComponentProvider());
return port;
}
private class ComponentProvider extends ComponentProviderAdapter {
public DockingPort createChildPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setComponentProvider(this);
return port;
}
public JSplitPane createSplitPane() {
JSplitPane split = new JSplitPane();
split.setDividerSize(3);
split.setBorder(null);
if (split.getUI() instanceof BasicSplitPaneUI) {
BasicSplitPaneDivider divider = ((BasicSplitPaneUI) split.getUI()).getDivider();
if (divider != null && divider.getBorder()!=null)
divider.setBorder(null);
}
return split;
}
}
public static void main(String[] args) {
JFrame f = new BorderDemo();
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Compound dockable window
package net.eleritec.docking.demos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import net.eleritec.docking.Dockable;
import net.eleritec.docking.DockableAdapter;
import net.eleritec.docking.DockingManager;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.DefaultDockingPort;
public class CompoundDemo extends JPanel {
private JLabel titlebar;
private Dockable dockableImpl;
public CompoundDemo(String title) {
super();
titlebar = createTitlebar(" " + title);
add(titlebar);
setBorder(new LineBorder(Color.black));
dockableImpl = new DockableImpl();
}
private JLabel createTitlebar(String title) {
JLabel lbl = new JLabel(title);
lbl.setForeground(Color.white);
lbl.setBackground(Color.blue);
lbl.setOpaque(true);
return lbl;
}
public void doLayout() {
Insets in = getInsets();
titlebar.setBounds(in.left, in.top, getWidth()-in.left-in.right, 25);
}
private CompoundDemo getThis() {
return this;
}
private Dockable getDockable() {
return dockableImpl;
}
private class DockableImpl extends DockableAdapter {
public Component getDockable() {
return getThis();
}
public String getDockableDesc() {
return titlebar.getText().trim();
}
public Component getInitiator() {
// the titlebar will the the "hot" component that initiates dragging
return titlebar;
}
}
private static JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort("North"), BorderLayout.NORTH);
p.add(buildDockingPort("South"), BorderLayout.SOUTH);
p.add(buildDockingPort("East"), BorderLayout.EAST);
p.add(buildDockingPort("West"), BorderLayout.WEST);
p.add(createDockingPort(), BorderLayout.CENTER);
return p;
}
private static DefaultDockingPort buildDockingPort(String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort();
// create the Dockable panel
CompoundDemo cd = new CompoundDemo(desc);
DockingManager.registerDockable(cd.getDockable());
// dock the panel and return the DockingPort
port.dock(cd.getDockable(), DockingPort.CENTER_REGION);
return port;
}
private static DefaultDockingPort createDockingPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setBackground(Color.gray);
port.setPreferredSize(new Dimension(100, 100));
return port;
}
public static void main(String[] args) {
JFrame f = new JFrame("Compound Docking Demo");
f.setContentPane(createContentPane());
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Cursor indication for dockable component
package net.eleritec.docking.demos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import net.eleritec.docking.CursorProvider;
import net.eleritec.docking.Dockable;
import net.eleritec.docking.DockableAdapter;
import net.eleritec.docking.DockingManager;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.DefaultDockingPort;
import net.eleritec.util.ResourceManager;
public class CursorDemo extends JPanel {
private JLabel titlebar;
private CursorProvider cursorProvider;
private Dockable dockableImpl;
public CursorDemo(String title) {
super();
titlebar = createTitlebar(" " + title);
add(titlebar);
setBorder(new LineBorder(Color.black));
dockableImpl = new DockableImpl();
}
private JLabel createTitlebar(String title) {
JLabel lbl = new JLabel(title);
lbl.setForeground(Color.white);
lbl.setBackground(Color.blue);
lbl.setOpaque(true);
return lbl;
}
public void doLayout() {
Insets in = getInsets();
titlebar.setBounds(in.left, in.top, getWidth() - in.left - in.right, 25);
}
private CursorDemo getThis() {
return this;
}
private void setCursorProvider(CursorProvider prov) {
cursorProvider = prov;
}
private Dockable getDockable() {
return dockableImpl;
}
private class DockableImpl extends DockableAdapter {
public CursorProvider getCursorProvider() {
return cursorProvider;
}
public Component getDockable() {
return getThis();
}
public String getDockableDesc() {
return titlebar.getText().trim();
}
public Component getInitiator() {
// the titlebar will the the "hot" component that initiates dragging
return titlebar;
}
}
private static JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort("North"), BorderLayout.NORTH);
p.add(buildDockingPort("South"), BorderLayout.SOUTH);
p.add(buildDockingPort("East"), BorderLayout.EAST);
p.add(buildDockingPort("West"), BorderLayout.WEST);
p.add(createDockingPort(), BorderLayout.CENTER);
return p;
}
private static DefaultDockingPort buildDockingPort(String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort();
// create the Dockable panel
CursorDemo cd = new CursorDemo(desc);
DockingManager.registerDockable(cd.getDockable());
// use a custom cursor provider for the north panel
if("North".equals(desc))
cd.setCursorProvider(new CursorDelegate());
// dock the panel and return the DockingPort
port.dock(cd.getDockable(), DockingPort.CENTER_REGION);
return port;
}
private static DefaultDockingPort createDockingPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setBackground(Color.gray);
port.setPreferredSize(new Dimension(100, 100));
return port;
}
public static void main(String[] args) {
JFrame f = new JFrame("Custom Cursor Docking Demo");
f.setContentPane(createContentPane());
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static class CursorDelegate implements CursorProvider {
private static final Image NORTH_IMG = createImg("tileHorizontal16.gif");
private static final Image EAST_IMG = createImg("tileVertical16.gif");
private static final Image CENTER_IMG = createImg("cascadeWindows16.gif");
private static final Image BLOCKED_IMG = createImg("closeAllWindows16.gif");
private static Image createImg(String imgName) {
return ResourceManager.createImage("resources/images/demo/" + imgName);
}
public Image getCenterImage() {
return CENTER_IMG;
}
public Image getDisallowedImage() {
return BLOCKED_IMG;
}
public Image getEastImage() {
return EAST_IMG;
}
public Image getNorthImage() {
return NORTH_IMG;
}
public Image getSouthImage() {
// same image as north
return NORTH_IMG;
}
public Image getWestImage() {
// same image as east
return EAST_IMG;
}
}
}
Elegant Dockable framework
package net.eleritec.docking.demos.elegant;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import net.eleritec.docking.Dockable;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.DefaultDockingPort;
import net.eleritec.docking.defaults.StandardBorderManager;
public class ElegantDemo extends JFrame {
private JSplitPane horizontalSplit;
private JSplitPane vertSplitRight;
private JSplitPane vertSplitLeft;
private ElegantPanel j2eeHierarchyView;
private ElegantPanel j2eeNavView;
private ElegantPanel consoleView;
private ElegantPanel serversView;
private ElegantPanel tasksView;
private ElegantPanel searchView;
private ElegantPanel synchronizeView;
private ElegantPanel outlineView;
private ElegantPanel editorView;
private ElegantDockingPort topLeft;
private ElegantDockingPort bottomLeft;
private ElegantDockingPort topRight;
private ElegantDockingPort bottomRight;
public ElegantDemo() {
super("Elegant Docking Demo");
init();
}
private void init() {
createViews();
horizontalSplit = createSplitPane(JSplitPane.HORIZONTAL_SPLIT);
vertSplitLeft = createSplitPane(JSplitPane.VERTICAL_SPLIT);
vertSplitRight = createSplitPane(JSplitPane.VERTICAL_SPLIT);
horizontalSplit.setLeftComponent(vertSplitLeft);
horizontalSplit.setRightComponent(vertSplitRight);
initDockingPorts();
setContentPane(horizontalSplit);
}
private void createViews() {
j2eeHierarchyView = new ElegantPanel("J2EE Hierarchy");
j2eeNavView = new ElegantPanel("J2EE Navigator");
consoleView = new ElegantPanel("Console");
serversView = new ElegantPanel("Servers");
tasksView = new ElegantPanel("Tasks");
searchView = new ElegantPanel("Search");
synchronizeView = new ElegantPanel("Synchronize");
outlineView = new ElegantPanel("Outline");
editorView = new ElegantPanel("Editor");
}
private void initDockingPorts() {
topLeft = new ElegantDockingPort();
bottomLeft = new ElegantDockingPort();
topRight = new ElegantDockingPort();
bottomRight = new ElegantDockingPort();
topLeft.add(j2eeHierarchyView);
topLeft.add(j2eeNavView);
bottomLeft.add(outlineView);
topRight.add(editorView);
bottomRight.add(tasksView);
bottomRight.add(serversView);
bottomRight.add(consoleView);
bottomRight.add(searchView);
bottomRight.add(synchronizeView);
vertSplitLeft.setLeftComponent(topLeft);
vertSplitLeft.setRightComponent(bottomLeft);
vertSplitRight.setLeftComponent(topRight);
vertSplitRight.setRightComponent(bottomRight);
}
private void postInit() {
horizontalSplit.setDividerLocation(0.3d);
vertSplitLeft.setDividerLocation(0.75d);
vertSplitRight.setDividerLocation(0.75d);
}
private static JSplitPane createSplitPane(int orientation) {
JSplitPane split = new JSplitPane(orientation);
// remove the border from the split pane
split.setBorder(null);
// set the divider size for a more reasonable, less bulky look
split.setDividerSize(3);
// check the UI. If we can"t work with the UI any further, then
// exit here.
if (!(split.getUI() instanceof BasicSplitPaneUI))
return split;
// grab the divider from the UI and remove the border from it
BasicSplitPaneDivider divider =
((BasicSplitPaneUI) split.getUI()).getDivider();
if (divider != null)
divider.setBorder(null);
return split;
}
public static void main(String[] args) {
ElegantDemo demo = new ElegantDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.setSize(800, 600);
demo.setVisible(true);
// now that we"re visible and validated, move the split pane
// dividers to their proper locations.
demo.postInit();
}
}
Simple demo for dockable windows
package net.eleritec.docking.demos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.eleritec.docking.DockingManager;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.DefaultDockingPort;
public class SimpleDemo extends JFrame {
public static void main(String[] args) {
JFrame f = new SimpleDemo();
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public SimpleDemo() {
super("Simple Docking Demo");
setContentPane(createContentPane());
}
private JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort(Color.blue, "Blue"), BorderLayout.NORTH);
p.add(buildDockingPort(Color.red, "Red"), BorderLayout.SOUTH);
p.add(buildDockingPort(Color.green, "Green"), BorderLayout.EAST);
p.add(buildDockingPort(Color.yellow, "Yellow"), BorderLayout.WEST);
p.add(createDockingPort(), BorderLayout.CENTER);
return p;
}
private DefaultDockingPort buildDockingPort(Color color, String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort();
// create and register the Dockable panel
JPanel p = new JPanel();
p.setBackground(color);
DockingManager.registerDockable(p, desc, true);
// dock the panel and return the DockingPort
port.dock(p, desc, DockingPort.CENTER_REGION, false);
return port;
}
private DefaultDockingPort createDockingPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setBackground(Color.gray);
port.setPreferredSize(new Dimension(100, 100));
return port;
}
}
SplitPane and dockable component
package net.eleritec.docking.demos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import net.eleritec.docking.Dockable;
import net.eleritec.docking.DockableAdapter;
import net.eleritec.docking.DockingManager;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.ruponentProviderAdapter;
import net.eleritec.docking.defaults.DefaultDockingPort;
public class SplitPaneDemo extends JPanel {
private JLabel titlebar;
private Dockable dockableImpl;
public SplitPaneDemo(String title) {
super();
titlebar = createTitlebar(" " + title);
add(titlebar);
setBorder(new LineBorder(Color.black));
dockableImpl = new DockableImpl();
}
private JLabel createTitlebar(String title) {
JLabel lbl = new JLabel(title);
lbl.setForeground(Color.white);
lbl.setBackground(Color.blue);
lbl.setOpaque(true);
return lbl;
}
public void doLayout() {
Insets in = getInsets();
titlebar.setBounds(in.left, in.top, getWidth()-in.left-in.right, 25);
}
private Dockable getDockable() {
return dockableImpl;
}
private SplitPaneDemo getThis() {
return this;
}
private class DockableImpl extends DockableAdapter {
public Component getDockable() {
return getThis();
}
public String getDockableDesc() {
return titlebar.getText().trim();
}
public Component getInitiator() {
// the titlebar will the the "hot" component that initiates dragging
return titlebar;
}
}
private static class ComponentProvider extends ComponentProviderAdapter {
public DockingPort createChildPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setComponentProvider(this);
return port;
}
public JSplitPane createSplitPane() {
JSplitPane split = new JSplitPane();
// remove the border from the split pane
split.setBorder(null);
// set the divider size for a more reasonable, less bulky look
split.setDividerSize(3);
// check the UI. If we can"t work with the UI any further, then
// exit here.
if (!(split.getUI() instanceof BasicSplitPaneUI))
return split;
// grab the divider from the UI and remove the border from it
BasicSplitPaneDivider divider = ((BasicSplitPaneUI) split.getUI()).getDivider();
if (divider != null)
divider.setBorder(null);
return split;
}
}
private static JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort("North"), BorderLayout.NORTH);
p.add(buildDockingPort("South"), BorderLayout.SOUTH);
p.add(buildDockingPort("East"), BorderLayout.EAST);
p.add(buildDockingPort("West"), BorderLayout.WEST);
p.add(createDockingPort(), BorderLayout.CENTER);
return p;
}
private static DefaultDockingPort buildDockingPort(String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort();
// create the Dockable panel
SplitPaneDemo spd = new SplitPaneDemo(desc);
DockingManager.registerDockable(spd.getDockable());
// dock the panel and return the DockingPort
port.dock(spd.getDockable(), DockingPort.CENTER_REGION);
return port;
}
private static DefaultDockingPort createDockingPort() {
DefaultDockingPort port = new DefaultDockingPort();
port.setBackground(Color.gray);
port.setPreferredSize(new Dimension(100, 100));
port.setComponentProvider(new ComponentProvider());
return port;
}
public static void main(String[] args) {
JFrame f = new JFrame("Split Docking Demo");
f.setContentPane(createContentPane());
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Swing dockable window
Swing dock framework
TabbedPane and dockable component 2
package net.eleritec.docking.demos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.LineBorder;
import net.eleritec.docking.Dockable;
import net.eleritec.docking.DockableAdapter;
import net.eleritec.docking.DockingManager;
import net.eleritec.docking.DockingPort;
import net.eleritec.docking.defaults.ruponentProviderAdapter;
import net.eleritec.docking.defaults.DefaultDockingPort;
public class TabbedPaneDemo extends JPanel {
private JLabel titlebar;
private Dockable dockableImpl;
public TabbedPaneDemo(String title) {
super();
titlebar = createTitlebar(" " + title);
add(titlebar);
setBorder(new LineBorder(Color.black));
dockableImpl = new DockableImpl();
}
private JLabel createTitlebar(String title) {
JLabel lbl = new JLabel(title);
lbl.setForeground(Color.white);
lbl.setBackground(Color.blue);
lbl.setOpaque(true);
return lbl;
}
public void doLayout() {
Insets in = getInsets();
titlebar.setBounds(in.left, in.top, getWidth() - in.left - in.right, 25);
}
private TabbedPaneDemo getThis() {
return this;
}
private Dockable getDockable() {
return dockableImpl;
}
private class DockableImpl extends DockableAdapter {
public Component getDockable() {
return getThis();
}
public String getDockableDesc() {
return titlebar.getText().trim();
}
public Component getInitiator() {
// the titlebar will the the "hot" component that initiates dragging
return titlebar;
}
}
private static JPanel createContentPane() {
JPanel p = new JPanel(new BorderLayout(5, 5));
p.add(buildDockingPort("North"), BorderLayout.NORTH);
p.add(buildDockingPort("South"), BorderLayout.SOUTH);
p.add(buildDockingPort("East"), BorderLayout.EAST);
p.add(buildDockingPort("West"), BorderLayout.WEST);
p.add(createDockingPort("Center"), BorderLayout.CENTER);
return p;
}
private static DefaultDockingPort buildDockingPort(String desc) {
// create the DockingPort
DefaultDockingPort port = createDockingPort(desc);
// create the Dockable panel
TabbedPaneDemo cd = new TabbedPaneDemo(desc);
DockingManager.registerDockable(cd.getDockable());
// dock the panel and return the DockingPort
port.dock(cd.getDockable(), DockingPort.CENTER_REGION);
return port;
}
private static int getTabPosition(String desc) {
if ("North".equals(desc))
return JTabbedPane.TOP;
if ("South".equals(desc))
return JTabbedPane.BOTTOM;
if ("East".equals(desc))
return JTabbedPane.RIGHT;
if ("West".equals(desc))
return JTabbedPane.LEFT;
return JTabbedPane.TOP;
}
private static DefaultDockingPort createDockingPort(String desc) {
DefaultDockingPort port = new DefaultDockingPort();
port.setBackground(Color.gray);
port.setPreferredSize(new Dimension(200, 100));
port.setComponentProvider(new ComponentProvider(getTabPosition(desc)));
return port;
}
public static void main(String[] args) {
JFrame f = new JFrame("Custom Conatainers Docking Demo");
f.setContentPane(createContentPane());
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
private static class ComponentProvider extends ComponentProviderAdapter {
int tabPosition;
private ComponentProvider(int tabs) {
tabPosition = tabs;
}
public JTabbedPane createTabbedPane() {
return new JTabbedPane(tabPosition);
}
}
}