Save This Page
Home » openjdk-7 » net.sourceforge » dictport » ui » [javadoc | source]
    1   /*
    2    *    Dictionary port to Java
    3    *    Copyright (C) 2000  Kaloian Doganov
    4    *
    5    *    This program is free software; you can redistribute it and/or modify
    6    *    it under the terms of the GNU General Public License as published by
    7    *    the Free Software Foundation; either version 2 of the License, or
    8    *    (at your option) any later version.
    9    *
   10    *    This program is distributed in the hope that it will be useful,
   11    *    but WITHOUT ANY WARRANTY; without even the implied warranty of
   12    *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13    *    GNU General Public License for more details.
   14   
   15    *    You should have received a copy of the GNU General Public License
   16    *    along with this program; if not, write to the Free Software
   17    *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18    */
   19   package net.sourceforge.dictport.ui;
   20   
   21   import java.awt;
   22   import java.awt.event;
   23   import javax.swing;
   24   import javax.swing.event;
   25   import net.sourceforge.dictport;
   26   
   27   /**
   28    * Similar UI Dialog
   29    *
   30    * @author Kaloian Doganov <kaloian@europe.com>
   31    */
   32   
   33   final class SimilarDialog extends JDialog {
   34   	
   35   	private JList fListBox;
   36   	private java.util.List fEntries;
   37   	private Action fTranslateAction;
   38   	private boolean fCanceled = true;
   39   	private String fWord;
   40   	
   41   	/**
   42   	 * Create Similar Words dialog with entries.
   43   	 *
   44   	 * @param aOwner Dialog owner.
   45   	 * @param aEntries List of Chapter.Entry objects.
   46   	 */
   47   	SimilarDialog(Frame aOwner, java.util.List aEntries) {
   48   		super(aOwner, "Similar Words", true);
   49   		fCanceled = true;
   50   		fWord = null;
   51   		fEntries = aEntries;
   52   		initComponents();
   53   		pack();
   54   		setResizable(false);
   55   	}
   56   	
   57   	private void initComponents() {
   58   		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
   59   		
   60   		getContentPane().setLayout(new FlowLayout());
   61   		JPanel root = new JPanel(new BorderLayout());
   62   		getContentPane().add(root);
   63   		
   64   		SelectionHandler selectionHandler = new SelectionHandler();
   65   		fListBox = new JList();
   66   		fListBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   67   		fListBox.setModel(new EntryModel(fEntries));
   68   		fListBox.addListSelectionListener(selectionHandler);
   69   		fListBox.addMouseListener(selectionHandler);
   70   		JScrollPane scrollPane = new JScrollPane(fListBox);
   71   		root.add(scrollPane, "Center");
   72   		
   73   		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   74   		
   75   		fTranslateAction = new TranslateAction();
   76   		
   77   		JButton translateButton = new JButton(fTranslateAction);
   78   		JButton closeButton = new JButton(new CloseAction());
   79   		
   80   		buttonPanel.add(translateButton);
   81   		buttonPanel.add(closeButton);
   82   		
   83   		root.add(buttonPanel, "South");
   84   		
   85   		getRootPane().setDefaultButton(translateButton);
   86   	}
   87   	
   88   	/** Return true if dialog is closed without translation. */
   89   	public boolean isCanceled() {
   90   		return fCanceled;
   91   	}
   92   	
   93   	/** Get selected word if dialog is not canceled. */
   94   	public String getWord() {
   95   		return fWord;
   96   	}
   97   	
   98   	/** Center window when showing. */
   99   	public void show() {
  100   		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
  101   		Rectangle window = this.getBounds(); 
  102   		window.x = (screen.width - window.width) / 2; 
  103   		window.y = (screen.height - window.height) / 2; 
  104   		this.setBounds(window);
  105   		super.show();
  106   	}
  107   	
  108   	/** ListModel using Chapter.Entry objects. */
  109   	private final class EntryModel extends DefaultListModel {
  110   		
  111   		/**
  112   		 * Create new EntryModel.
  113   		 *
  114   		 * @param aList list of Chapter.Entry objects.
  115   		 */
  116   		EntryModel(java.util.List aList) {
  117   			super();
  118   			java.util.Iterator iterator = aList.iterator();
  119   			while (iterator.hasNext())
  120   				addElement(((Chapter.Entry) iterator.next()).getKeyDisplay());
  121   		}
  122   	}
  123   	
  124   	/** Handle list selection events. */
  125   	private final class SelectionHandler extends MouseAdapter
  126   		implements ListSelectionListener, MouseListener {
  127   			
  128   		public void valueChanged(ListSelectionEvent aEvent) {
  129   			fTranslateAction.setEnabled(fListBox.getSelectedIndex() != -1);
  130   		}
  131   		
  132   		public void mouseClicked(MouseEvent aEvent) {
  133   			// handle double-click on the selection list
  134   			if (aEvent.getClickCount() == 2) {
  135   				int index = fListBox.getSelectedIndex();
  136   				if (index != -1) fTranslateAction.actionPerformed(null);
  137   				aEvent.consume();
  138   			}
  139   		}
  140   	}
  141   	
  142   	/** Translate action. */
  143   	private final class TranslateAction extends AbstractAction {
  144   		public TranslateAction() {
  145   			super("Translate");
  146   			setEnabled(false);
  147   		}
  148   		
  149   		public void actionPerformed(ActionEvent aEvent) {
  150   			ListModel model = fListBox.getModel();
  151   			fWord = (String) model.getElementAt(fListBox.getSelectedIndex());
  152   			fCanceled = false;
  153   			dispose();
  154   		}
  155   	}
  156   	
  157   	/** Close action. */
  158   	private final class CloseAction extends AbstractAction {
  159   		
  160   		CloseAction() {
  161   			super("Close");
  162   		}
  163   		
  164   		public void actionPerformed(ActionEvent aEvent) {
  165   			dispose();
  166   		}
  167   	}
  168   	
  169   	/** Self test method. */
  170   	public static void main(String[] args) {
  171   		SimilarDialog dialog = new SimilarDialog(null,
  172   			new java.util.ArrayList());
  173   		dialog.show();
  174   	}	
  175   }

Save This Page
Home » openjdk-7 » net.sourceforge » dictport » ui » [javadoc | source]