📄 cadpane.java
字号:
//Create/set menu bar and content pane.
CADPane pane = new CADPane();
pane.setOpaque(true); //content panes must be opaque
//Create and set up the window.
JFrame myFrame = new JFrame(CADResource.APPLICATION_TITLE);
myFrame.setJMenuBar(pane.createMenuBar());
//myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.addWindowListener(new CloseWindowAdapter(pane));
myFrame.setContentPane(pane);
//Display the window.
myFrame.pack();
myFrame.setVisible(true);
pane.openFiles(files);
//* demo
LibHelper helper =LibHelper.getInstance();
helper.showLibraryDialog(pane);
//*/
}
public void closeAllWindows(){
JInternalFrame[] frames = m_desktop.getAllFrames();
for (int i = 0; i < frames.length; i++){
try{
frames[i].setClosed(true);
}catch(Exception e){}
}
}
/** get currently focused internal frame in cad pane.
* @return currently focused internal frame.
*/
public DrawFrame getCurrentFrame(){
try{
//get the desktop of this cad pane.
JDesktopPane desktop =getDesktop();
//currently active frame within this desktop.
JInternalFrame iFrame =desktop.getSelectedFrame();
if (iFrame!=null && iFrame instanceof DrawFrame)
return (DrawFrame)iFrame;
else
return null;
}catch(Exception e){
return null;
}
}
/** get current drawing canvas in focused internal frame.
* @return current drawing canvas.
*/
public DrawCanvas getDrawCanvas(){
//get current focused internal frame.
DrawFrame drawFrame =getCurrentFrame();
if (drawFrame!=null)
return drawFrame.getDrawPane().getDrawCanvas();
else
return null;
}
/** clear the '*' charactor that represents a modified page.*/
private String clearTitleModifier(String title){
if (title==null || title.equals(""))
return "";
if (title.endsWith(" *")){
return title.substring(0,title.length()-2);
}else{
return title;
}
}
/** Append all window titles to window menu for furthur toggle operations.
*/
public void appendWindowTitles(){
List titleList =new ArrayList();
JInternalFrame[] frames = m_desktop.getAllFrames();
for (int i = 0; i < frames.length; i++){
JInternalFrame iFrame =frames[i];
String title =clearTitleModifier(iFrame.getTitle());
titleList.add(title);
}
m_toolFactory.appendTitlesToWindowMenu(titleList,this);
}
/** Focus window by title
* @param title A specified window title.
*/
public void focusWindowByTitle(String title){
JInternalFrame[] frames = m_desktop.getAllFrames();
title =clearTitleModifier(title);
for (int i = 0; i < frames.length; i++){
JInternalFrame iFrame =frames[i];
String thisTitle =clearTitleModifier(iFrame.getTitle());
if (thisTitle.equals(title)){
try{
iFrame.setSelected(true);
iFrame.toFront();
return;
}catch(Exception e){
}
}
}
}
/** new file operation.
* This operation should generate a new drawing page.
*/
public void newFile(){
DrawFrame newFrame = newDrawFrame("");
if (newFrame!=null){
newFrame.setVisible(true);
setLayerName(JFLayer.LAYER_NAME_DEFAULT);
appendWindowTitles();
}
}
/** create a new draw frame */
private DrawFrame newDrawFrame(String title){
JDesktopPane desktop =getDesktop();
if (desktop==null)
return null;
DrawFrame newFrame = new DrawFrame(this,title);
newFrame.setSize(new Dimension(600, 400));
desktop.add(newFrame);
try {
newFrame.setMaximum(true);
newFrame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
return newFrame;
}
/** get a frame with a title.
* @return the frame found.
*/
private DrawFrame getFrame(String title){
//get the desktop of this cad pane.
JDesktopPane desktop =getDesktop();
JInternalFrame[] frameAry =desktop.getAllFrames();
if (frameAry==null || frameAry.length==0)
return null;
for (int i=0; i<frameAry.length; i++){
DrawFrame frame =(DrawFrame)frameAry[i];
if (frame.isTitleNamed() && frame.getCurrentTitle().equals(title))
return frame;
}
return null;
}
/** open a specified file
* @param fileName A spcified file name
*/
public boolean openFile(String fileName){
if (fileName==null || fileName.equals(""))
return false;
try{
//if this file is already opened in other frame.
DrawFrame frame =getFrame(fileName);
if (frame!=null){
frame.setSelected(true);
return true;
}else{
frame = newDrawFrame(fileName);
}
//tell draw pane to open file.
frame.getDrawPane().openFile(fileName);
DrawCanvas drawCanvas =frame.getDrawPane().getDrawCanvas();
if (drawCanvas==null)
return false;
JFPage page =drawCanvas.getDrawPage();
frame.setVisible(true);
JFLayer layer=page.getCurrentLayer();
if (layer!=null){
setLayerName(layer.getName());
}
setFileName(fileName);
return true;
}catch(Exception e){
m_logger.error("openFile: "+e);
return false;
}
}
/** Save current page to a specified file.
* @param fileName A file to be saved into.
*/
public boolean saveFile(String fileName){
if (fileName==null || fileName.equals(""))
return false;
try{
DrawCanvas drawCanvas =getDrawCanvas();
if (drawCanvas==null)
return false;
JFPage page =drawCanvas.getDrawPage();
if (fileName.endsWith(JFFileFilter.FILEEXT_DRAW)){
//binary JFDraw file
if (!page.saveToBinary(fileName))
return false;
}else{
//xml JFDraw file.
if (!page.saveToXML(fileName))
return false;
}
//set global path.
GlobalSettings m_settings =GlobalSettings.getInstance();
m_settings.setGlobalPath(fileName);
setFileName(fileName);
return true;
} catch(Exception e) {
m_logger.error("saveFile: "+e);
}
return false;
}
/** get the file name of current opened page.
* @return the file name
*/
public String getFileName(){
DrawCanvas drawCanvas =getDrawCanvas();
if (drawCanvas==null)
return "";
else
return drawCanvas.getFileName();
}
/** set the file name of current opened page.
* @param fileName A new file name
*/
public void setFileName(String fileName){
DrawFrame frame =getCurrentFrame();
if (frame==null)
return;
frame.setTitle(fileName);
appendWindowTitles();
}
/** test */
public static void main (final String args[]){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI(args);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -