Skip to content

Commit

Permalink
1. implemented a trivial in-memory image cache for the time being - b…
Browse files Browse the repository at this point in the history
…etter than nothing

2. added options to config panel so that mouse actions are actually configurable
  • Loading branch information
zhanshi committed Dec 6, 2007
1 parent 384b018 commit 88c2917
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 43 deletions.
16 changes: 15 additions & 1 deletion src/image/ImageFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package image;
import java.net.*;
import java.awt.image.BufferedImage;
import java.util.Hashtable;
import javax.imageio.*;

/**
Expand All @@ -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);
Expand Down
26 changes: 17 additions & 9 deletions src/powerreader/ConfigUI.form
Original file line number Diff line number Diff line change
Expand Up @@ -186,28 +186,39 @@
<Component class="javax.swing.JComboBox" name="m_combo_leftClick">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="1">
<StringItem index="0" value="Focus On Item"/>
<StringArray count="4">
<StringItem index="0" value="Focus on item"/>
<StringItem index="1" value="Drag and scroll"/>
<StringItem index="2" value="Read dictionary definition"/>
<StringItem index="3" value="Do nothing"/>
</StringArray>
</Property>
</Properties>
</Component>
<Component class="javax.swing.JComboBox" name="m_combo_middleClick">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="1">
<StringItem index="0" value="Read Dictionary Definition"/>
<StringArray count="4">
<StringItem index="0" value="Focus on item"/>
<StringItem index="1" value="Drag and scroll"/>
<StringItem index="2" value="Read dictionary definition"/>
<StringItem index="3" value="Do nothing"/>
</StringArray>
</Property>
<Property name="selectedIndex" type="int" value="2"/>
</Properties>
</Component>
<Component class="javax.swing.JComboBox" name="m_combo_rightClick">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="1">
<StringItem index="0" value="Drag and Scroll"/>
<StringArray count="4">
<StringItem index="0" value="Focus on item"/>
<StringItem index="1" value="Drag and scroll"/>
<StringItem index="2" value="Read dictionary definition"/>
<StringItem index="3" value="Do nothing"/>
</StringArray>
</Property>
<Property name="selectedIndex" type="int" value="1"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="m_okButton">
Expand Down Expand Up @@ -307,9 +318,6 @@
</Component>
<Component class="javax.swing.JSlider" name="m_slider_lineWidth">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Tahoma" size="11" style="0"/>
</Property>
<Property name="majorTickSpacing" type="int" value="10"/>
<Property name="minimum" type="int" value="5"/>
<Property name="minorTickSpacing" type="int" value="20"/>
Expand Down
12 changes: 8 additions & 4 deletions src/powerreader/ConfigUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions src/powerreader/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
72 changes: 43 additions & 29 deletions src/powerreader/Pick.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}
}
Expand All @@ -160,15 +164,25 @@ 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();
}
}

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;
}
}
Expand Down

0 comments on commit 88c2917

Please sign in to comment.