📄 libpanel.java
字号:
/**
* $Id:LibPanel.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfdraw.gui.dialog;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.BorderFactory;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JPopupMenu;
import javax.swing.JFileChooser;
import com.jfimagine.jfdraw.gui.ToolFactory;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GUIConst;
import com.jfimagine.jfdraw.gui.ToolFactory;
import com.jfimagine.jfdraw.gui.dialog.LabelDialog;
import com.jfimagine.jfdraw.gui.DrawAdapter;
import com.jfimagine.jfdraw.gui.DrawCanvas;
import com.jfimagine.jfdraw.action.FileAction;
import com.jfimagine.jfgraph.transfer.JFFileFilter;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.shape.union.AbstractLibrary;
import com.jfimagine.jfgraph.shape.union.JFLibrary;
import com.jfimagine.jfgraph.shape.union.JFTemplate;
import com.jfimagine.jfgraph.shape.union.JFLibElem;
import com.jfimagine.jfgraph.shape.union.JFPage;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.transfer.JFClipboard;
import com.jfimagine.jfgraph.shape.rectangle.JFImage;
import com.jfimagine.utils.log.*;
/**
* LibPanel is an internal tabbed page for LibDialog.
*
* @author CookieMaker
*
* @version $Revision: 1.1.0 $
*/
public class LibPanel extends JPanel implements ActionListener,ComponentListener, ItemListener{
/** A button click command. */
public static final String CMD_BTN_CLICK ="cmd.button.click";
/** A new library command. */
public static final String CMD_LIB_NEW ="cmd.library.new";
/** A modify library command. */
public static final String CMD_LIB_MODIFY ="cmd.library.modify";
/** A load library command. */
public static final String CMD_LIB_LOAD ="cmd.library.load";
/** A load url library command. */
public static final String CMD_LIB_LOADURL ="cmd.library.loadurl";
/** A save library command. */
public static final String CMD_LIB_SAVE ="cmd.library.save";
/** A remove library command. */
public static final String CMD_LIB_REMOVE ="cmd.library.remove";
/** insert current library/template to drawing canvas. */
public static final String CMD_LIB_CURR_INSERTINTO ="cmd.lib.curr.insertinto";
/** remove current library/template from library list. */
public static final String CMD_LIB_CURR_REMOVE ="cmd.lib.curr.remove";
/**an internal log utility*/
private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
private JComboBox m_combo;
private JButton m_btn;
private LibCanvas m_canvas;
private JPopupMenu m_menu;
private JPanel bottomPanel;
private JScrollPane drawScrollPane;
/** library type definition, Library or Template*/
public static final int TYPE_LIBRARY =1;
public static final int TYPE_TEMPLATE =2;
/** library type */
private int m_type =TYPE_LIBRARY;
/** libraries or templates loaded in this panel*/
private List m_libList =new ArrayList();
private int m_currLibIndex =0;
/** file extension, file description */
private String m_fileExt="";
private String m_fileDesc="";
/**We need to prevent combo changing in some situations*/
private boolean m_preventComboChanging =false;
private DrawAdapter m_pane;
/**
* Constructor
* @param type Library or template type.
* @param pane A DrawAdapter panel for reference.
*/
public LibPanel(int type,DrawAdapter pane) {
m_type =type;
m_pane =pane;
m_menu =new JPopupMenu();
m_menu.setFont(GUIConst.FONT_MENU);
if (m_type==TYPE_LIBRARY){
addLibraryMenus(m_menu,this);
m_fileExt =JFFileFilter.FILEEXT_DRAW_LIBRARY;
m_fileDesc=JFFileFilter.FILEDESC_DRAW_LIBRARY;
}else{
addTemplateMenus(m_menu,this);
m_fileExt =JFFileFilter.FILEEXT_DRAW_TEMPLATE;
m_fileDesc=JFFileFilter.FILEDESC_DRAW_TEMPLATE;
}
// lib item combobox
m_combo =new JComboBox();
m_combo.setFont(GUIConst.FONT_LABEL);
m_combo.addItemListener(this);
m_combo.setPreferredSize(new Dimension(50,25));
//distable light weight combobox popup features, to avoid recursively repaint event on libcanvas.
m_combo.setLightWeightPopupEnabled(false);
// lib process button
ImageIcon icon =ToolFactory.createIcon("general/library16");
m_btn =new JButton(icon);
m_btn.setPreferredSize(new Dimension(25,25));
m_btn.setActionCommand(CMD_BTN_CLICK);
m_btn.addActionListener(this);
//Create a container to add combobox and button.
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
topPanel.setPreferredSize(new Dimension(100,25));
topPanel.add(m_combo);
topPanel.add(m_btn);
// draw canvas
m_canvas=new LibCanvas(type,this);
// scroll pane
drawScrollPane = new JScrollPane(m_canvas);
bottomPanel =new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(drawScrollPane,BorderLayout.CENTER);
//add to parent panel.
//setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
//add(topPanel);
//add(bottomPanel);
setLayout(new BorderLayout());
add(topPanel,BorderLayout.NORTH);
add(bottomPanel,BorderLayout.CENTER);
bottomPanel.addComponentListener(this);
}
/** get current library index*/
public int getCurrLibIndex(){
return m_currLibIndex;
}
/** set current library index*/
public void setCurrLibIndex(int index){
m_currLibIndex =index;
if (index<0){
comboItemChanged();
}else if (m_combo.getSelectedIndex()!=index){
try{
m_preventComboChanging =true;
m_combo.setSelectedIndex(index);
}catch(Exception e){
}
m_preventComboChanging =false;
comboItemChanged();
}
}
/** get library type, i.e. library or template.
* @return library type, TYPE_LIBRARY or TYPE_TEMPLATE.
*/
public int getType(){
return m_type;
}
/** get current active library/template.
* @return current active library.
*/
public AbstractLibrary getCurrentLibrary(){
return m_canvas.getLibrary();
}
private void comboItemChanged(){
try{
if (m_preventComboChanging)
return;
m_currLibIndex =m_combo.getSelectedIndex();
if (m_currLibIndex>=0){
AbstractLibrary lib=(AbstractLibrary)m_libList.get(m_currLibIndex);
m_canvas.setLibrary(lib);
}else{
m_canvas.setLibrary(null);
}
m_canvas.repaint();
}catch(Exception e){
}
}
/** repaint components*/
public void paint(Graphics g){
super.paint(g);
m_canvas.repaint();
}
/** resize components*/
public void resizeComponents(){
Rectangle r =bottomPanel.getVisibleRect();
int width =(int)r.getWidth();
int height =(int)r.getHeight();
Dimension d =new Dimension(width-5,height-5);
drawScrollPane.setPreferredSize(new Dimension(d));
drawScrollPane.setSize(new Dimension(d));
AbstractLibrary lib =m_canvas.getLibrary();
if (lib==null){
d =new Dimension(width-8,height-8);
m_canvas.setPreferredSize(new Dimension(d));
m_canvas.setSize(new Dimension(d));
}else{
d =new Dimension(width-8,height-8);
d =lib.getDrawSize(d);
m_canvas.setPreferredSize(new Dimension(d));
m_canvas.setSize(new Dimension(d));
}
m_canvas.repaint();
}
/** Process actions from menus/buttons.
*
* @param e An action event sent by menus/buttons.
*
*/
public void actionPerformed(ActionEvent e) {
String cmd =e.getActionCommand();
if (CMD_BTN_CLICK.equals(cmd)) //click btn
popupLibMenu(e);
else if (CMD_LIB_LOAD.equals(cmd))
loadLibraryFile();
else if (CMD_LIB_LOADURL.equals(cmd))
loadURLLibraryFile();
else if (CMD_LIB_NEW.equals(cmd))
newLibrary();
else if (CMD_LIB_MODIFY.equals(cmd))
modifyLibrary();
else if (CMD_LIB_SAVE.equals(cmd))
saveLibrary();
else if (CMD_LIB_REMOVE.equals(cmd))
removeLibrary();
else if (CMD_LIB_CURR_INSERTINTO.equals(cmd))
insertCurrLibInto();
else if (CMD_LIB_CURR_REMOVE.equals(cmd))
removeCurrLib();
}
/** get the index of a filename if already loaded*/
private int getFilenameIndex(String filename){
if (filename==null || filename.length()==0)
return -1;
Iterator it =m_libList.iterator();
int cnt=0;
while (it!=null && it.hasNext()){
AbstractLibrary lib =(AbstractLibrary)it.next();
if (filename.equals(lib.getFilename())){
return cnt;
}
cnt++;
}
return -1;
}
/** show all combo items from library list*/
private void showComboItems(){
try{
m_combo.removeAllItems();
Iterator it =m_libList.iterator();
while (it!=null && it.hasNext()){
AbstractLibrary lib =(AbstractLibrary)it.next();
m_combo.addItem(lib.getTitle());
}
}catch(Exception e){
}
}
/** If this library/template is modified
* @return True if modified, false otherwise.
*/
public boolean isModified(){
Iterator it =m_libList.iterator();
while (it!=null && it.hasNext()){
AbstractLibrary lib =(AbstractLibrary)it.next();
if (lib.isModified())
return true;
}
return false;
}
/** load library from a local binary library file.
*/
private void loadLibraryFile(){
//consider the path of a currently opened file.
String fileName="";
if (m_libList.size()>0){
try{
AbstractLibrary lib =(AbstractLibrary)m_libList.get(m_libList.size()-1);
fileName =lib.getFilename();
}catch(Exception e){
}
}
String currDir=System.getProperty("user.dir");
if (fileName!=null && fileName.length()>0){
currDir =fileName;
}
JFileChooser fc=new JFileChooser(currDir);
JFFileFilter filter = new JFFileFilter();
filter.addExtension(m_fileExt);
filter.setDescription(m_fileDesc);
fc.setFileFilter(filter);
//show dialog
int fd = fc.showOpenDialog(this);
if(fd==JFileChooser.APPROVE_OPTION){
fileName =fc.getSelectedFile().getAbsolutePath();
}else{
return;
}
loadLibrary(fileName);
}
/** load library from an URL binary library file.
*/
private void loadURLLibraryFile(){
String fileName ="";
if (m_type==TYPE_LIBRARY)
fileName =javax.swing.JOptionPane.showInputDialog(CADResource.getString("label.filetype.web.library"), "http://");
else
fileName =javax.swing.JOptionPane.showInputDialog(CADResource.getString("label.filetype.web.template"), "http://");
loadLibrary(fileName);;
}
/** load library from a binary library file.
*/
public void loadLibrary(String fileName){
if (fileName==null || fileName.equals(""))
return;
try{
int index =getFilenameIndex(fileName);
if (index>=0){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -