?? draggablecomponentbox.java
字號:
/*
* Copyright (C) 2004 NNL Technology AB
* Visit www.infonode.net for information about InfoNode(R)
* products and how to contact NNL Technology AB.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
// $Id: DraggableComponentBox.java,v 1.52 2005/12/04 13:46:03 jesper Exp $
package net.infonode.gui.draggable;
import net.infonode.gui.*;
import net.infonode.gui.layout.DirectionLayout;
import net.infonode.gui.panel.SimplePanel;
import net.infonode.util.Direction;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
public class DraggableComponentBox extends SimplePanel {
private boolean componentBoxEnabled = true;
private JComponent componentBox;
private JComponent componentContainer;
private JComponent outerParentArea = this;
private Direction componentDirection = Direction.UP;
private boolean scrollEnabled = false;
private boolean ensureSelectedVisible;
private boolean autoSelect = true;
private boolean descendingSortOrder = true;
private boolean doReverseSort = false;
private boolean mustSort = false;
private int scrollOffset;
private int iconSize;
private DraggableComponent selectedComponent;
private DraggableComponent topComponent;
private ArrayList listeners;
private ArrayList draggableComponentList = new ArrayList(10);
private ArrayList layoutOrderList = new ArrayList(10);
private ScrollButtonBox scrollButtonBox;
private boolean useDefaultScrollButtons = true;
private DraggableComponentListener draggableComponentListener = new DraggableComponentListener() {
public void changed(DraggableComponentEvent event) {
if (event.getType() == DraggableComponentEvent.TYPE_MOVED) {
sortComponentList(!descendingSortOrder);
}
fireChangedEvent(event);
}
public void selected(DraggableComponentEvent event) {
doSelectComponent(event.getSource());
}
public void dragged(DraggableComponentEvent event) {
fireDraggedEvent(event);
}
public void dropped(DraggableComponentEvent event) {
ensureSelectedVisible();
fireDroppedEvent(event);
}
public void dragAborted(DraggableComponentEvent event) {
ensureSelectedVisible();
fireNotDroppedEvent(event);
}
};
public DraggableComponentBox(int iconSize) {
this(iconSize, true);
}
public DraggableComponentBox(int iconSize, boolean useDefaultScrollButtons) {
this.iconSize = iconSize;
this.useDefaultScrollButtons = useDefaultScrollButtons;
// Fix minimum size when flipping direction
final DirectionLayout layout = new DirectionLayout(componentDirection == Direction.UP ? Direction.RIGHT : componentDirection == Direction.LEFT
?
Direction.DOWN
:
componentDirection == Direction.DOWN ?
Direction.RIGHT :
Direction.DOWN) {
public Dimension minimumLayoutSize(Container parent) {
Dimension min = super.minimumLayoutSize(parent);
Dimension pref = super.preferredLayoutSize(parent);
return componentDirection.isHorizontal() ?
new Dimension(pref.width, min.height) : new Dimension(min.width, pref.height);
}
public void layoutContainer(Container parent) {
if (DraggableComponentBox.this != null && componentBoxEnabled) {
//long millis = System.currentTimeMillis();
doSort();
super.layoutContainer(parent);
//System.out.println("Layout: " + (System.currentTimeMillis() - millis));
}
}
public Dimension preferredLayoutSize(Container parent) {
doSort();
return super.preferredLayoutSize(parent);
}
};
layout.setLayoutOrderList(layoutOrderList);
componentBox = new SimplePanel(layout) {
public boolean isOptimizedDrawingEnabled() {
return DraggableComponentBox.this != null && getComponentSpacing() >= 0;
}
};
componentBox.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
//fireChangedEvent();
}
public void componentMoved(ComponentEvent e) {
fireChangedEvent();
}
});
initialize();
}
public void addListener(DraggableComponentBoxListener listener) {
if (listeners == null)
listeners = new ArrayList(2);
listeners.add(listener);
}
public void removeListener(DraggableComponentBoxListener listener) {
if (listeners != null) {
listeners.remove(listener);
if (listeners.size() == 0)
listeners = null;
}
}
public void addDraggableComponent(DraggableComponent component) {
insertDraggableComponent(component, -1);
}
public void insertDraggableComponent(DraggableComponent component, int index) {
component.setLayoutOrderList(layoutOrderList);
component.addListener(draggableComponentListener);
if (index < 0) {
layoutOrderList.add(component.getComponent());
componentBox.add(component.getComponent());
}
else {
layoutOrderList.add(index, component.getComponent());
componentBox.add(component.getComponent(), index);
}
sortComponentList(!descendingSortOrder);
draggableComponentList.add(component);
component.setOuterParentArea(outerParentArea);
componentBox.revalidate();
fireAddedEvent(component);
if (autoSelect && layoutOrderList.size() == 1 && selectedComponent == null && component.isEnabled())
doSelectComponent(component);
updateScrollButtons();
}
private void updateScrollButtons() {
if (scrollButtonBox != null) {
ScrollableBox scrollableBox = (ScrollableBox) componentContainer;
scrollButtonBox.setButton1Enabled(!scrollableBox.isLeftEnd());
scrollButtonBox.setButton2Enabled(!scrollableBox.isRightEnd());
}
}
public void insertDraggableComponent(DraggableComponent component, Point p) {
int componentIndex = getComponentIndexAtPoint(p);
if (componentIndex != -1 && layoutOrderList.size() > 0)
insertDraggableComponent(component, componentIndex);
else
insertDraggableComponent(component, -1);
}
public void selectDraggableComponent(DraggableComponent component) {
if (component == null) {
if (selectedComponent != null) {
DraggableComponent oldSelected = selectedComponent;
selectedComponent = null;
fireSelectedEvent(selectedComponent, oldSelected);
//componentBox.repaint();
}
}
else
component.select();
}
public void removeDraggableComponent(DraggableComponent component) {
if (component != null && draggableComponentList.contains(component)) {
//component.abortDrag();
int index = layoutOrderList.indexOf(component.getComponent());
component.removeListener(draggableComponentListener);
if (component == topComponent)
topComponent = null;
if (layoutOrderList.size() > 1 && selectedComponent != null) {
if (selectedComponent == component) {
if (autoSelect) {
int selectIndex = findSelectableComponentIndex(index);
if (selectIndex > -1)
selectDraggableComponent(findDraggableComponent((Component) layoutOrderList.get(selectIndex)));
else
selectedComponent = null;
}
else {
selectDraggableComponent(null);
}
}
}
else {
if (selectedComponent != null) {
DraggableComponent oldSelected = selectedComponent;
selectedComponent = null;
fireSelectedEvent(selectedComponent, oldSelected);
}
}
draggableComponentList.remove(component);
layoutOrderList.remove(component.getComponent());
componentBox.remove(component.getComponent());
componentBox.revalidate();
//componentBox.validate();
component.setLayoutOrderList(null);
sortComponentList(!descendingSortOrder);
updateScrollButtons();
fireRemovedEvent(component);
}
}
public boolean containsDraggableComponent(DraggableComponent component) {
return draggableComponentList.contains(component);
}
public DraggableComponent getSelectedDraggableComponent() {
return selectedComponent;
}
public int getDraggableComponentCount() {
return layoutOrderList.size();
}
public DraggableComponent getDraggableComponentAt(int index) {
return index < layoutOrderList.size() ? findDraggableComponent((Component) layoutOrderList.get(index)) : null;
}
public int getDraggableComponentIndex(DraggableComponent component) {
return layoutOrderList.indexOf(component.getComponent());
}
public Object[] getDraggableComponents() {
return draggableComponentList.toArray();
}
public Component[] getBoxComponents() {
return componentBox.getComponents();
}
public boolean getDepthSortOrder() {
return descendingSortOrder;
}
public void setDepthSortOrder(boolean descending) {
if (descending != this.descendingSortOrder) {
this.descendingSortOrder = descending;
sortComponentList(!descending);
doSort();
}
}
public boolean isScrollEnabled() {
return scrollEnabled;
}
public void setScrollEnabled(boolean scrollEnabled) {
if (scrollEnabled != this.scrollEnabled) {
this.scrollEnabled = scrollEnabled;
initialize();
}
}
public int getScrollOffset() {
return scrollOffset;
}
public void setScrollOffset(int scrollOffset) {
if (scrollOffset != this.scrollOffset) {
this.scrollOffset = scrollOffset;
if (scrollEnabled)
((ScrollableBox) componentContainer).setScrollOffset(scrollOffset);
}
}
public int getComponentSpacing() {
return getDirectionLayout().getComponentSpacing();
}
public void setComponentSpacing(int componentSpacing) {
if (componentSpacing != getDirectionLayout().getComponentSpacing()) {
if (getComponentSpacing() < 0 && componentSpacing >= 0) {
DraggableComponent tmp = topComponent;
sortComponentList(false);
topComponent = tmp;
}
getDirectionLayout().setComponentSpacing(componentSpacing);
sortComponentList(!descendingSortOrder);
componentBox.revalidate();
}
}
public boolean isEnsureSelectedVisible() {
return ensureSelectedVisible;
}
public void setEnsureSelectedVisible(boolean ensureSelectedVisible) {
this.ensureSelectedVisible = ensureSelectedVisible;
}
public boolean isAutoSelect() {
return autoSelect;
}
public void setAutoSelect(boolean autoSelect) {
this.autoSelect = autoSelect;
}
public Direction getComponentDirection() {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -