From 88c29171e2e914d7b1d1dbc01af49ed5ec461f4d Mon Sep 17 00:00:00 2001 From: zhanshi Date: Thu, 6 Dec 2007 06:49:37 +0000 Subject: [PATCH] 1. implemented a trivial in-memory image cache for the time being - better than nothing 2. added options to config panel so that mouse actions are actually configurable --- src/image/ImageFetcher.java | 16 ++++- src/powerreader/ConfigUI.form | 26 +++++--- src/powerreader/ConfigUI.java | 12 ++-- src/powerreader/ConfigurationManager.java | 27 +++++++++ src/powerreader/Pick.java | 72 ++++++++++++++--------- 5 files changed, 110 insertions(+), 43 deletions(-) diff --git a/src/image/ImageFetcher.java b/src/image/ImageFetcher.java index 052e5b7..b9d3946 100644 --- a/src/image/ImageFetcher.java +++ b/src/image/ImageFetcher.java @@ -10,6 +10,7 @@ package image; import java.net.*; import java.awt.image.BufferedImage; +import java.util.Hashtable; import javax.imageio.*; /** @@ -19,13 +20,26 @@ public abstract class ImageFetcher { protected String imageFetcherName; + protected Hashtable imageCache; + + public ImageFetcher() { + imageCache = new Hashtable(); + } public abstract String getImageURL(String text); - public BufferedImage getImage(String imgURL) { + + public BufferedImage getImage(String text) { + if (imageCache.containsKey(text)) { + return (BufferedImage) imageCache.get(text); + } BufferedImage img; + String imgURL = getImageURL(text); + System.out.println("Fetching image from " + imgURL); try { URL url = new URL(imgURL); img = ImageIO.read(url); + // save the image for future use + imageCache.put(text, img); return img; } catch (Exception e) { System.out.println("Unable to retrieve image from " + imgURL); diff --git a/src/powerreader/ConfigUI.form b/src/powerreader/ConfigUI.form index 0056bfd..b1af198 100644 --- a/src/powerreader/ConfigUI.form +++ b/src/powerreader/ConfigUI.form @@ -186,8 +186,11 @@ - - + + + + + @@ -195,19 +198,27 @@ - - + + + + + + - - + + + + + + @@ -307,9 +318,6 @@ - - - diff --git a/src/powerreader/ConfigUI.java b/src/powerreader/ConfigUI.java index dc8a565..bf5ae16 100644 --- a/src/powerreader/ConfigUI.java +++ b/src/powerreader/ConfigUI.java @@ -64,11 +64,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { } }); - m_combo_leftClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Focus On Item" })); + m_combo_leftClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Focus on item", "Drag and scroll", "Read dictionary definition", "Do nothing" })); - m_combo_middleClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Read Dictionary Definition" })); + m_combo_middleClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Focus on item", "Drag and scroll", "Read dictionary definition", "Do nothing" })); + m_combo_middleClick.setSelectedIndex(2); - m_combo_rightClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Drag and Scroll" })); + m_combo_rightClick.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Focus on item", "Drag and scroll", "Read dictionary definition", "Do nothing" })); + m_combo_rightClick.setSelectedIndex(1); m_okButton.setText("OK"); m_okButton.addActionListener(new java.awt.event.ActionListener() { @@ -114,7 +116,6 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { m_label_rightClick1.setText("Line Width "); m_label_rightClick1.setName("Line Width"); - m_slider_lineWidth.setFont(new java.awt.Font("Tahoma", 0, 11)); m_slider_lineWidth.setMajorTickSpacing(10); m_slider_lineWidth.setMinimum(5); m_slider_lineWidth.setMinorTickSpacing(20); @@ -233,6 +234,9 @@ private void m_okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI } ConfigurationManager.setDictionary(m_combo_dictionaryLookup.getSelectedIndex()); ConfigurationManager.setImageLookup(m_combo_imageLookup.getSelectedIndex()); + ConfigurationManager.setLeftClickAction(m_combo_leftClick.getSelectedIndex()); + ConfigurationManager.setMiddleClickAction(m_combo_middleClick.getSelectedIndex()); + ConfigurationManager.setRightClickAction(m_combo_rightClick.getSelectedIndex()); this.setVisible(false); }//GEN-LAST:event_m_okButtonActionPerformed diff --git a/src/powerreader/ConfigurationManager.java b/src/powerreader/ConfigurationManager.java index dfdc2ad..44c9c35 100644 --- a/src/powerreader/ConfigurationManager.java +++ b/src/powerreader/ConfigurationManager.java @@ -52,6 +52,14 @@ public class ConfigurationManager { private boolean wordsGrow = false; private boolean followFocus = false; + public static final int ACTION_FOCUS = 0; + public static final int ACTION_DRAG = 1; + public static final int ACTION_DEFINE = 2; + public static final int ACTION_NOTHING = 3; + static private int leftClickAction = ACTION_FOCUS; + static private int middleClickAction = ACTION_DEFINE; + static private int rightClickAction = ACTION_DRAG; + // Word focus level by default static private int m_focusLevel = 3; static private int m_detailLevel = 0; @@ -171,6 +179,25 @@ static public boolean followFocus() { return m_instance.followFocus; } + static public void setLeftClickAction(int a) { + leftClickAction = a; + } + static public int getLeftClickAction() { + return leftClickAction; + } + static public void setMiddleClickAction(int a) { + middleClickAction = a; + } + static public int getMiddleClickAction() { + return middleClickAction; + } + static public void setRightClickAction(int a) { + rightClickAction = a; + } + static public int getRightClickAction() { + return rightClickAction; + } + static public void refreshTranslate() { if(m_instance.m_mainTransformGroup != null) { // Cap current_z if we need to diff --git a/src/powerreader/Pick.java b/src/powerreader/Pick.java index 0f3c922..f84eb0c 100644 --- a/src/powerreader/Pick.java +++ b/src/powerreader/Pick.java @@ -85,8 +85,18 @@ public void mouseClicked(MouseEvent e) { String pickedText=pickedObject.getValue(); System.out.println(pickedText); + // determine action for the button + int mouseAction = ConfigurationManager.ACTION_NOTHING; + if (e.getButton() == MouseEvent.BUTTON1) { + mouseAction = ConfigurationManager.getLeftClickAction(); + } else if (e.getButton() == MouseEvent.BUTTON2) { + mouseAction = ConfigurationManager.getMiddleClickAction(); + } else if (e.getButton() == MouseEvent.BUTTON3) { + mouseAction = ConfigurationManager.getRightClickAction(); + } // select word - if(e.getButton() == MouseEvent.BUTTON1 || e.getButton() == MouseEvent.BUTTON2) { + if(mouseAction == ConfigurationManager.ACTION_FOCUS || + mouseAction == ConfigurationManager.ACTION_DEFINE) { // Clear the focus highlight Player.getFocusOn().color(false); @@ -100,41 +110,35 @@ public void mouseClicked(MouseEvent e) { Player.playOne(); } // dictionary meaning and images - if(e.getButton() == MouseEvent.BUTTON2) { - - // Get and speak dictionary definition + if(mouseAction == ConfigurationManager.ACTION_DEFINE) { DictionaryLookup w = ConfigurationManager.getDictionary(); String def = w.getDefinition(pickedText); String toSpeak = "Definition of " + pickedText + ". " + def; System.out.println(toSpeak); Speech.speak(toSpeak); + } + // Get image + if(ConfigurationManager.showImages()) { + ImageFetcher f = ConfigurationManager.getImageFetcher(); + TextureLoader imageT = new TextureLoader(f.getImage(pickedText),c3D); + Raster imageObj = new Raster(new Point3f(0, 0,1f), + Raster.RASTER_COLOR, 0, 0, imageT.getImage().getWidth(), imageT.getImage().getHeight(), + imageT.getImage(), null); + Shape3D shape = new Shape3D(imageObj); + imageObj.setCapability(Raster.ALLOW_IMAGE_WRITE); + BranchGroup node = new BranchGroup(); + + node.setCapability(BranchGroup.ALLOW_DETACH); + + node.addChild(shape); + lastPicked = tObj.getTheTextTransformGroup(); + lastPicked.addChild(node); - // Get image - if(ConfigurationManager.showImages()) { - ImageFetcher f = ConfigurationManager.getImageFetcher(); - String url = f.getImageURL(pickedText); - System.out.println(url); - URL mURL= new URL(url); - TextureLoader imageT = new TextureLoader(mURL,c3D); - Raster imageObj = new Raster(new Point3f(0, 0,1f), - Raster.RASTER_COLOR, 0, 0, imageT.getImage().getWidth(), imageT.getImage().getHeight(), - imageT.getImage(), null); - Shape3D shape = new Shape3D(imageObj); - imageObj.setCapability(Raster.ALLOW_IMAGE_WRITE); - BranchGroup node = new BranchGroup(); - - node.setCapability(BranchGroup.ALLOW_DETACH); - - node.addChild(shape); - lastPicked = tObj.getTheTextTransformGroup(); - lastPicked.addChild(node); - - lastAttached =node; - } + lastAttached =node; } } } - } catch (MalformedURLException ex) { + } catch (Exception ex) { ex.printStackTrace(); } } @@ -160,7 +164,12 @@ public void mouseDragged(MouseEvent e) { } public void mousePressed(MouseEvent e) { - if(e.getButton() == MouseEvent.BUTTON3) { + if((e.getButton() == MouseEvent.BUTTON1 && + ConfigurationManager.getLeftClickAction() == ConfigurationManager.ACTION_DRAG) || + (e.getButton() == MouseEvent.BUTTON2 && + ConfigurationManager.getMiddleClickAction() == ConfigurationManager.ACTION_DRAG) || + (e.getButton() == MouseEvent.BUTTON3 && + ConfigurationManager.getRightClickAction() == ConfigurationManager.ACTION_DRAG)) { mousePressed = true; lastX = e.getX(); lastY = e.getY(); @@ -168,7 +177,12 @@ public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { - if(e.getButton() == MouseEvent.BUTTON3) { + if((e.getButton() == MouseEvent.BUTTON1 && + ConfigurationManager.getLeftClickAction() == ConfigurationManager.ACTION_DRAG) || + (e.getButton() == MouseEvent.BUTTON2 && + ConfigurationManager.getMiddleClickAction() == ConfigurationManager.ACTION_DRAG) || + (e.getButton() == MouseEvent.BUTTON3 && + ConfigurationManager.getRightClickAction() == ConfigurationManager.ACTION_DRAG)) { mousePressed = false; } }