Package simtools.ui

Source Code of simtools.ui.UserPropertiesEditor

/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*     Nicolas Brodu
*
*
* $Id: UserPropertiesEditor.java,v 1.5 2006/11/23 17:11:00 ogor Exp $
*
* Changes
* -------
*
*/

package simtools.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.PlainDocument;
import javax.swing.undo.UndoManager;

import simtools.diagram.undo.UndoHandler;

/**
* A simple editor for user properties including
* import/export and edit with undo/redo/cut/copy/paste
*
* @author Claude CAZENAVE
*
*/
public class UserPropertiesEditor extends JDialog implements CaretListener,ActionListener {

  /** Resources */
  public static MenuResourceBundle resources = ResourceFinder
      .getMenu(UserPropertiesEditor.class);

  UndoHandler undoHandler;
  AbstractAction copyAction;
  AbstractAction cutAction;
  AbstractAction pasteAction;
 
  JMenuItem importMenuItem;
  JMenuItem exportMenuItem;
  JMenuItem closeMenuItem;
 
  JTextArea ui;
 
  PlainDocument doc;
 
  JFileChooser chooser;
 
  public UserPropertiesEditor(Frame owner) {
    super(owner,true);
    chooser=null;
    getContentPane().setLayout(new BorderLayout());
    JScrollPane scroller = new JScrollPane();
    JViewport port = scroller.getViewport();
   
    doc=new PlainDocument();
    createTextArea();
    port.add(ui);
   
    undoHandler = new UndoHandler(new UndoManager());
    doc.addUndoableEditListener(undoHandler);
    ui.addCaretListener(this);
    setupActions();

    JMenuBar mb = new JMenuBar();
    mb.add(createFileMenu());
    mb.add(createEditMenu());
    setJMenuBar(mb);
   
    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(createToolBar(), BorderLayout.NORTH);
    pack();
    setSize(600, 480);
  }
 
  public boolean show(UserProperties up){
    if(chooser==null){
      chooser=new JFileChooser(up.getFile().getParentFile());
    }
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    try{
      up.store(baos,"");
    } catch(IOException ioe){
      ioe.printStackTrace();
    }
    try {
      doc.insertString(0,baos.toString(),null);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
    setTitle(resources.getString("titlePart1")+up._productName+resources.getString("titlePart2"));
    show(); // this will block
    return !baos.toString().equals(getText());
  }
 
  protected void createTextArea(){
    ui=new JTextArea(doc);
  }
 
  protected JToolBar createToolBar(){
    JToolBar res=new JToolBar();
    res.add(undoHandler.getUndoAction());
    res.add(undoHandler.getRedoAction());
    res.add(cutAction);
    res.add(copyAction);
    res.add(pasteAction);
    return res;
  }

  protected JMenu createFileMenu(){
    JMenu m=resources.getMenu("file");
    importMenuItem=resources.getItem("import",this);
    m.add(importMenuItem);
    exportMenuItem=resources.getItem("export",this);
    m.add(exportMenuItem);
    closeMenuItem=resources.getItem("close",this);
    m.add(closeMenuItem);
    return m;
  }
 
  protected JMenu createEditMenu(){
    JMenu m=resources.getMenu("edit");
    m.add(undoHandler.getUndoAction());
    m.add(undoHandler.getRedoAction());
   
    m.addSeparator();
   
    m.add(cutAction);
    m.add(copyAction);
    m.add(pasteAction);
   
    return m;
  }
 
  public String getText(){
    try {
      return doc.getText(0,doc.getLength());
    } catch (BadLocationException e) {
      e.printStackTrace();
      return "";
    }
  }
 
  public void update(UserProperties up){
    ByteArrayInputStream bais=new ByteArrayInputStream(getText().replaceAll("\r","").getBytes());
    try{
      up.load(bais);
    }
    catch(IOException ioe){
      ioe.printStackTrace();
    }
  }
 
  /**
   * Display the user properties in the editor
   * @param up the user properties to display
   * @param confirm is true to ask for confirmation
   * @return
   */
  public boolean showAndUpdate(UserProperties up, boolean confirm){
    return showAndUpdate(up,confirm,null);
  }
 
  /**
   * Display the user properties in the editor
   * @param up the user properties to display
   * @param confirm is true to ask for confirmation
   * @param restart name of the application to restart or null
   * @return
   */
  public boolean showAndUpdate(UserProperties up, boolean confirm, String restart){
    if(show(up)){
      if(!confirm || JOptionPane.showConfirmDialog(this,
            (restart==null) ? resources.getString("confirmMessage")
                : resources.getString("confirmRestartMessage1")+
                restart+resources.getString("confirmRestartMessage2"),
            resources.getString("confirmTitle"),
            JOptionPane.YES_NO_OPTION)
          ==JOptionPane.OK_OPTION){
        update(up);
        return true;
      }
    }
    return false;
  }
 
  protected void setupActions(){
    copyAction=(AbstractAction) ui.getActionMap().get(DefaultEditorKit.copyAction);
    copyAction.putValue(AbstractAction.SMALL_ICON,
          resources.getIcon("copyImage"));
    copyAction.putValue(AbstractAction.NAME,
        resources.getString("copyAction"));
    copyAction.putValue(AbstractAction.ACCELERATOR_KEY,
        resources.getKeyStroke("copyShortcut"));
    copyAction.putValue(AbstractAction.LONG_DESCRIPTION,
        resources.getString("copyTip"));
    cutAction=(AbstractAction) ui.getActionMap().get(DefaultEditorKit.cutAction);
    cutAction.putValue(AbstractAction.SMALL_ICON,
        resources.getIcon("cutImage"));
    cutAction.putValue(AbstractAction.NAME,
        resources.getString("cutAction"));
    cutAction.putValue(AbstractAction.ACCELERATOR_KEY,
        resources.getKeyStroke("cutShortcut"));
    cutAction.putValue(AbstractAction.LONG_DESCRIPTION,
        resources.getString("cutTip"));
    pasteAction=(AbstractAction) ui.getActionMap().get(DefaultEditorKit.pasteAction);
    pasteAction.putValue(AbstractAction.SMALL_ICON,
        resources.getIcon("pasteImage"));
    pasteAction.putValue(AbstractAction.NAME,
        resources.getString("pasteAction"));
    pasteAction.putValue(AbstractAction.ACCELERATOR_KEY,
        resources.getKeyStroke("pasteShortcut"));
    pasteAction.putValue(AbstractAction.LONG_DESCRIPTION,
        resources.getString("pasteTip"));
   
    cutAction.setEnabled(false);
    copyAction.setEnabled(false);
    pasteAction.setEnabled(true);   

    undoHandler.getUndoAction().putValue(AbstractAction.ACCELERATOR_KEY,
        resources.getKeyStroke("undoShortcut"));
    undoHandler.getRedoAction().putValue(AbstractAction.ACCELERATOR_KEY,
        resources.getKeyStroke("redoShortcut"));
  }
 
  /* (non-Javadoc)
   * @see javax.swing.event.CaretListener#caretUpdate(javax.swing.event.CaretEvent)
   */
  public void caretUpdate(CaretEvent e) {
    boolean sel=e.getDot()!=e.getMark();
    cutAction.setEnabled(sel);
    copyAction.setEnabled(sel);
  }

  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    if(e.getSource()==importMenuItem){
      if (chooser.showDialog(this,resources.getString("importTitle")) == JFileChooser.APPROVE_OPTION){
        read(chooser.getSelectedFile());
      }
    }
    else if(e.getSource()==exportMenuItem){
      if (chooser.showDialog(this,resources.getString("exportTitle")) == JFileChooser.APPROVE_OPTION){
        write(chooser.getSelectedFile());
      }
    }
    else if(e.getSource()==closeMenuItem){
      this.hide();
    }
  }

  protected void write(File f){
    try {
      FileWriter fw=new FileWriter(f);
      fw.write(getText());
      fw.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  protected void read(File f){
    String sr=System.getProperty("line.separator");
    BufferedReader in=null;
    try {
      in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "8859_1"));
    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();
    }
    StringBuffer sb=new StringBuffer();
   
    while (in!=null) {
      // Get next line
      String line;
      try {
        line = in.readLine();
      } catch (IOException e2) {
        break;
      }
      if (line == null)
        break;
      sb.append(line);
      sb.append(sr);
    }
    try {
      doc.remove(0,doc.getLength()-1);
      doc.insertString(0,sb.toString(),null);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
 
  /**
   * For testing
   * @param args
   */
  public static void main(String[] args) {
    final UserPropertiesEditor editor=new UserPropertiesEditor(null);
    UserProperties up=new UserProperties("jsynoptic");
    up.read();
    if(editor.showAndUpdate(up, true)){
      up.write();
    }
    System.exit(0);
  }
}
TOP

Related Classes of simtools.ui.UserPropertiesEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.