📄 objectpalette.java
字号:
/*
* This file is part of Caliph & Emir.
*
* Caliph & Emir 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.
*
* Caliph & Emir 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 Caliph & Emir; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright statement:
* --------------------
* (c) 2005 by Werner Klieber (werner@klieber.info)
* http://caliph-emir.sourceforge.net
*/
package at.wklieber.gui;
import at.wklieber.Settings;
import at.wklieber.gui.data.IComponentData;
import at.wklieber.tools.*;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.jdom.Element;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
/**
* @author Werner Klieber
* @version 1.0
*/
public class ObjectPalette extends JDialog
{
private static Category cat = Logger.getLogger(ObjectPalette.class.getName());
private static Console console = Console.getReference();
//private static Java2dTools java2dTools = Java2dTools.getReference();
private static Mpeg7ConversionTools mpeg7Convert = Mpeg7ConversionTools.getReference();
private static Settings cfg = Settings.getReference();
public static final int PANEL_COLOR = 0;
public static final int PANEL_SHAPE = 1;
public static final int PANEL_IMAGE = 2;
public static final int PANEL_IMAGE_LIST = 3;
public static final int PANEL_REPRESENTANT = 4;
// size of one component to draw.
// is limited by drawComponents to draw a label
private int elementWidth = 40;
private int elementHeigh = 60;
private int gap = 5;
JPanel mainPanel = null; // This panel covers the complete drawing Frame
private Container contentPane = null; // the contentPane of the Frame
private JDialog mainFrame = null;
private IDrawPanel drawPanel = null;
private Container toolbarWrapper = null;
private JMenuBar menuBar = null;
private JPanel statusPanel = null;
private JToolBar toolBar = null;
//private JComboBox imageCombo = null;
private MenuTools menuTool = null;
private JLabel statusBar;
private IComponent[] imageArray = new IComponent[0];
private IComponent[] representantArray = new IComponent[0];
private IComponent[] colorArray = new IComponent[0];
private IComponent[] shapeArray = new IComponent[0];
private IComponent[] imageListArray = new IComponent[0];
private java.util.List<ImageListStruct> imageListCache = null; // List of IComponent[] Pictures that are read in from directories
private String imageListBaseDir = null;
private int currentDisplay = -1;
private static final String SELECT_SAMPLE_IMAGES = "Select sample images";
private MouseListener parentMouseListener;
//private int currentListEntry = -1;
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
}
public ObjectPalette() {
super();
init(null);
}
// does not work right: the elements are not drawn correctly
public ObjectPalette(JPanel mainPanel) {
super();
init(mainPanel);
}
// use this to display only a own set of components
// NOTE: not fully implemented yet. Will not work
/* public ObjectPalette(IComponent[] componentArray1) {
super();
cat.debug("not fully implemented yet. Will not work");
init(componentArray1);
}*/
private void init(JPanel a_mainFrame) {
boolean isStandalone;
parentMouseListener = null;
// System.out.println("Main: " + a_mainFrame.getSize());
if (a_mainFrame != null) {
mainFrame = null;
contentPane = a_mainFrame;
isStandalone = false;
} else {
mainFrame = this;
mainFrame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
mainFrame.setSize(new Dimension(400, 400));
mainFrame.setTitle("Object Palette");
contentPane = mainFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
isStandalone = true;
}
//mainFrame.setVisible(true);
if (mainPanel != null) {
mainPanel.removeAll();
}
// initialize all member-menu Panels
mainPanel = new JPanel(); // This panel covers the complete Frame
mainPanel.setSize(contentPane.getSize());
drawPanel = new IDrawPanel();
drawPanel.setSize(contentPane.getSize());
drawPanel.setAcceptDnd(false);
menuBar = new JMenuBar();
statusPanel = new JPanel();
//buttonPanel = new JPanel();
toolbarWrapper = new Container();
LayoutManager mgr = new FlowLayout(FlowLayout.LEADING);
toolbarWrapper.setLayout(mgr);
//------------------------ add Elements ---------------------------
String iconLocation = cfg.getIconsDir();
menuTool = new MenuTools(this, menuBar, toolbarWrapper, iconLocation);
setMenuEntries();
//------------------------ define the Drawpanel ---------------------------
//drawPanel.setMinimumSize(new Dimension(200, 200));
drawPanel.setIgnoreRepaint(false);
if (isStandalone) {
drawPanel.setPreferredSize(new Dimension(400, 400));
}
drawPanel.setToolTipText("");
//drawPanel.setBackground(Color.white);
//drawPanel.setBorder(BorderFactory.createLineBorder(Color.black));
drawPanel.setBorder(BorderFactory.createRaisedBevelBorder());
drawPanel.setLayout(null);
drawPanel.add(new Scrollbar(Scrollbar.VERTICAL));
//--------------- link all panels to the frame
mainPanel.setLayout(new BorderLayout());
if (isStandalone) {
mainFrame.setJMenuBar(menuBar);
contentPane.add(toolbarWrapper, BorderLayout.NORTH);
Component bottomPanel = buildBottomPanel();
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
}
contentPane.add(mainPanel, BorderLayout.CENTER);
mainPanel.add(drawPanel, BorderLayout.CENTER);
if (isStandalone) {
mainFrame.validate();
mainFrame.repaint();
//setIComponents(componentArray1);
mainFrame.setVisible(true); // show before drawing objects, because otherwise they are not valid
// show the representants as default
//actionChooseRepresentant(null);
} else {
// System.out.println("Main: " + mainPanel.getSize());
// System.out.println("content: " + contentPane.getSize());
// System.out.println("drawPanel: " + drawPanel.getSize());
actionChooseColor(null); // choose the color-objects as default
contentPane.validate();
contentPane.repaint();
contentPane.setVisible(true);
// System.out.println("Main: " + mainPanel.getSize());
// System.out.println("content: " + contentPane.getSize());
// System.out.println("drawPanel: " + drawPanel.getSize());
}
} // end method init
private Component buildBottomPanel() {
statusBar = new JLabel(" Idle...");
statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
return statusBar;
}
public JPanel getDrawPanel() {
return (JPanel) drawPanel;
}
// inserts the menu and toolbar stuff
private void setMenuEntries() {
//menuTool.readMenuFromConfigFile("objectDialog", MenuTools.TYPE_MENUBAR);
//---- add a combobox with all directories in the image directory to the menu
/*if (imageListCache == null) {
imageListCache = new Vector<ImageListStruct>();
}
java.util.Vector<String> lastDirEntry = new Vector<String>();
lastDirEntry.add(SELECT_SAMPLE_IMAGES);
imageListBaseDir = cfg.getImageDir();
String[] dirs = new String[0]; // Array with all sub directory names*/
/*if (cfg.isStartedAsJarFile()) {
String archive = cfg.getBaseFile();
String directory = cfg.getImageDir();
String dir = directory.substring(archive.length());
archive = archive.substring(0, archive.length() - 2);
try {
dirs = UnzipTools.getFileList(archive, dir, false);
} catch (Exception e) {
cat.debug(e);
}
} else {*/
//dirs = FileTools.subDirList(new FileParserSettings(imageListBaseDir, "*.*"));
//}
// sort out directories containing no images
/*java.util.List<String> dirList = new Vector<String>(dirs.length, 10);
for (int i = 0; i < dirs.length; i++) {
String[] dummy = getImagesNamesFromDirectory(imageListBaseDir + dirs[i] + "/");
if (dummy.length > 0) {
dirList.add(dirs[i]);
}
}
dirs = (String[]) dirList.toArray(new String[dirList.size()]);
for (int i = 0; i < dirs.length; i++) {
lastDirEntry.add(dirs[i]);
//cat.debug("Como add: " + dirs[i]);
ImageListStruct data = new ImageListStruct(new IComponent[0], dirs[i]);
imageListCache.add(data);
}
*/
/*imageCombo = new JComboBox(lastDirEntry);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String selected = (String) cb.getSelectedItem();
int index = (int) cb.getSelectedIndex();
actionImageArraySelected(selected, index);
}
};
imageCombo.addActionListener(listener);
menuTool.addToolBarEntry(imageCombo);
*/
//menuBar.add(imageCombo);
}
/**
* an entry of the compobox has been clicked
*/
private void actionImageArraySelected(String selected, int index) {
//cat.debug("Selected: " + index);
//drawPanel.removeAll();
if (index < 0) {
//autodetect image:
for (int i = 0; i < imageListCache.size(); i++) {
ImageListStruct struct = imageListCache.get(i);
if (struct.getTitle().equalsIgnoreCase(selected)) {
index = i;
break;
}
}
//return;
}
if (index == 0 && selected.equalsIgnoreCase(SELECT_SAMPLE_IMAGES)) {
return;
}
currentDisplay = PANEL_IMAGE_LIST;
//currentListEntry = index;
IComponent[] compArray = null;
if (index < imageListCache.size()) {
try {
ImageListStruct data = imageListCache.get(index);
compArray = data.getImages();
} catch (Exception e) {
cat.debug(e);
}
}
if (compArray == null || compArray.length == 0) {
compArray = getImagesFromDirectory(imageListBaseDir + selected + "/");
ImageListStruct data = new ImageListStruct(new IComponent[0], selected);
imageListCache.add(index, data);
}
imageListArray = compArray;
//validateTree();
//repaint();
//drawComponents();
mainFrame.validate();
mainFrame.repaint();
}
public void actionSelectImageArray(String entryName) {
actionImageArraySelected(entryName, -1);
}
// import images from a user given directory to the combobox
public void actionImportImages(ActionEvent e) {
//cat.debug("Selected: " + index);
//drawPanel.removeAll();
//String selected = "5";
String dir = FileTools.showOpenDialog("", "jpg", "Select the image import Directory", true, "");
dir = FileTools.getFilePath(dir);
cat.debug("DIR: " + dir);
if (dir.length() == 0) {
return;
}
imageFromDirToPalette(dir);
}
// import images from a given directory to the combobox
private void imageFromDirToPalette(String dir) {
if (getImagesNamesFromDirectory(dir, false).length < 1) {
MessageBox.displayMessage("Information", "No images found in directory \"" + dir + "\"");
return;
}
String[] filePathNameList = FileTools.parsePathName(dir);
String entryName = "Unknown";
if (filePathNameList.length > 0) {
entryName = filePathNameList[filePathNameList.length - 1];
}
currentDisplay = PANEL_IMAGE_LIST;
//int index = imageCombo.getItemCount(); // index is zero based.
//imageCombo.addItem(entryName);
//imageCombo.setSelectedIndex(index);
/*ImageListStruct data = new ImageListStruct(new IComponent[0], entryName);
imageListCache.add(data); // just for updating the reading process
IComponent[] images = getImagesFromDirectory(dir, false);
data = new ImageListStruct(images, entryName);
//currentListEntry = index; // not really used
imageListCache.add(index, data); // add new IComponent[]
imageListArray = images; // current components to draw
*/
//validateTree();
//repaint();
//drawComponents();
mainFrame.validate();
mainFrame.repaint();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -