Line 0
Link Here
|
|
|
1 |
/* |
2 |
* To change this template, choose Tools | Templates |
3 |
* and open the template in the editor. |
4 |
*/ |
5 |
package org.netbeans.modules.image.preview; |
6 |
|
7 |
import java.awt.image.BufferedImage; |
8 |
import java.io.IOException; |
9 |
import java.io.InputStream; |
10 |
import java.util.Collection; |
11 |
import javax.imageio.ImageIO; |
12 |
import javax.swing.JComponent; |
13 |
import javax.swing.SwingUtilities; |
14 |
import org.netbeans.spi.navigator.NavigatorPanel; |
15 |
import org.openide.loaders.DataObject; |
16 |
import org.openide.util.Exceptions; |
17 |
import org.openide.util.Lookup; |
18 |
import org.openide.util.LookupEvent; |
19 |
import org.openide.util.LookupListener; |
20 |
|
21 |
/** |
22 |
* |
23 |
* @author joshis |
24 |
*/ |
25 |
public class ImageNavigatorProvider implements NavigatorPanel { |
26 |
|
27 |
private ImagePanel panelUI; |
28 |
private static final Lookup.Template MY_DATA = new Lookup.Template(Object.class); |
29 |
private Lookup.Result curContext; |
30 |
private LookupListener contextL; |
31 |
|
32 |
/** public no arg constructor needed for system to instantiate provider well */ |
33 |
public ImageNavigatorProvider() { |
34 |
} |
35 |
|
36 |
public String getDisplayHint() { |
37 |
return org.openide.util.NbBundle.getMessage(ImageNavigatorProvider.class, "DISPLAY_HINT"); |
38 |
} |
39 |
|
40 |
public String getDisplayName() { |
41 |
return org.openide.util.NbBundle.getMessage(ImageNavigatorProvider.class, "DISPLAY_NAME"); |
42 |
} |
43 |
|
44 |
public JComponent getComponent() { |
45 |
if (panelUI == null) { |
46 |
panelUI = new ImagePanel(); |
47 |
} |
48 |
return panelUI; |
49 |
} |
50 |
|
51 |
public void panelActivated(Lookup context) { |
52 |
curContext = context.lookup(MY_DATA); |
53 |
curContext.addLookupListener(getContextListener()); |
54 |
Collection data = curContext.allInstances(); |
55 |
setNewContent(data); |
56 |
} |
57 |
|
58 |
public void panelDeactivated() { |
59 |
curContext.removeLookupListener(getContextListener()); |
60 |
curContext = null; |
61 |
} |
62 |
|
63 |
public Lookup getLookup() { |
64 |
return null; |
65 |
} |
66 |
|
67 |
/************* non - public part ************/ |
68 |
private void setNewContent(Collection newData) { |
69 |
InputStream is = null; |
70 |
DataObject fo = null; |
71 |
try { |
72 |
while (newData.iterator().hasNext()) { |
73 |
Object o = newData.iterator().next(); |
74 |
if (o instanceof DataObject) { |
75 |
fo = (DataObject) o; |
76 |
break; |
77 |
} |
78 |
} |
79 |
if (fo == null) return; |
80 |
is = fo.getPrimaryFile().getInputStream(); |
81 |
final BufferedImage bim = ImageIO.read(is); |
82 |
SwingUtilities.invokeLater(new Runnable() { |
83 |
|
84 |
public void run() { |
85 |
panelUI.setImage(bim); |
86 |
panelUI.revalidate(); |
87 |
panelUI.repaint(); |
88 |
} |
89 |
}); |
90 |
} catch (IOException ex) { |
91 |
Exceptions.printStackTrace(ex); |
92 |
} finally { |
93 |
try { |
94 |
is.close(); |
95 |
} catch (IOException ex) { |
96 |
Exceptions.printStackTrace(ex); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
/** Accessor for listener to context */ |
102 |
private LookupListener getContextListener() { |
103 |
if (contextL == null) { |
104 |
contextL = new ContextListener(); |
105 |
} |
106 |
return contextL; |
107 |
} |
108 |
|
109 |
/** Listens to changes of context and triggers proper action */ |
110 |
private class ContextListener implements LookupListener { |
111 |
|
112 |
public void resultChanged(LookupEvent ev) { |
113 |
Collection data = ((Lookup.Result) ev.getSource()).allInstances(); |
114 |
setNewContent(data); |
115 |
} |
116 |
} // end of ContextListener |
117 |
} |