Added
Link Here
|
1 |
package org.netbeans.modules.java.api.common.project.ui; |
2 |
|
3 |
import java.awt.Cursor; |
4 |
import java.awt.EventQueue; |
5 |
import java.nio.charset.Charset; |
6 |
import java.text.MessageFormat; |
7 |
import java.util.Map; |
8 |
import java.util.SortedMap; |
9 |
import javax.swing.DefaultComboBoxModel; |
10 |
import org.netbeans.api.queries.FileEncodingQuery; |
11 |
import org.openide.DialogDescriptor; |
12 |
import org.openide.DialogDisplayer; |
13 |
import org.openide.util.HelpCtx; |
14 |
import org.openide.util.NbBundle; |
15 |
import org.openide.util.RequestProcessor; |
16 |
|
17 |
/** |
18 |
* |
19 |
* @author Tim Boudreau |
20 |
*/ |
21 |
final class UnsupportedEncodingDialog extends javax.swing.JPanel implements Runnable { |
22 |
|
23 |
private final DefaultComboBoxModel mdl = new DefaultComboBoxModel(); |
24 |
private DialogDescriptor descriptor; |
25 |
|
26 |
/** Creates new form UnsupportedEncodingDialog */ |
27 |
public UnsupportedEncodingDialog() { |
28 |
initComponents(); |
29 |
Cursor cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); |
30 |
setCursor(cursor); |
31 |
instructions.setCursor(cursor); |
32 |
charsetComboBox.setCursor(cursor); |
33 |
} |
34 |
|
35 |
void setDialogDescriptor(DialogDescriptor d) { |
36 |
this.descriptor = d; |
37 |
} |
38 |
|
39 |
public static Charset showDialog(String projectName, String requestedCharset) { |
40 |
UnsupportedEncodingDialog pnl = new UnsupportedEncodingDialog(); |
41 |
String s = pnl.instructions.getText(); |
42 |
MessageFormat.format(s, new Object[] { requestedCharset }); |
43 |
String title = NbBundle.getMessage(UnsupportedEncodingDialog.class, |
44 |
"UNSUPPORTED_ENCODING", requestedCharset, projectName); //NO18N |
45 |
String close = NbBundle.getMessage(UnsupportedEncodingDialog.class, |
46 |
"CLOSE"); //NOI18N |
47 |
DialogDescriptor des = new DialogDescriptor (pnl, |
48 |
title, true, new Object[] { close }, |
49 |
close, DialogDescriptor.DEFAULT_ALIGN, |
50 |
HelpCtx.DEFAULT_HELP, null); //NOI18N |
51 |
pnl.setDialogDescriptor(des); |
52 |
DialogDisplayer.getDefault().notify(des); |
53 |
return pnl.getSelectedCharset(); |
54 |
} |
55 |
|
56 |
private Charset getSelectedCharset() { |
57 |
Object o = charsetComboBox.getSelectedItem(); |
58 |
if (o instanceof Entry) { |
59 |
return ((Entry) o).charset; |
60 |
} else { |
61 |
//should never happen |
62 |
return FileEncodingQuery.getDefaultEncoding(); |
63 |
} |
64 |
} |
65 |
|
66 |
/** This method is called from within the constructor to |
67 |
* initialize the form. |
68 |
* WARNING: Do NOT modify this code. The content of this method is |
69 |
* always regenerated by the Form Editor. |
70 |
*/ |
71 |
@SuppressWarnings("unchecked") |
72 |
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
73 |
private void initComponents() { |
74 |
java.awt.GridBagConstraints gridBagConstraints; |
75 |
|
76 |
instructions = new javax.swing.JLabel(); |
77 |
charsetComboBox = new javax.swing.JComboBox(); |
78 |
|
79 |
setLayout(new java.awt.GridBagLayout()); |
80 |
|
81 |
instructions.setLabelFor(charsetComboBox); |
82 |
instructions.setText(org.openide.util.NbBundle.getMessage(UnsupportedEncodingDialog.class, "UnsupportedEncodingDialog.instructions.text", new Object[] {})); // NOI18N |
83 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
84 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
85 |
gridBagConstraints.weightx = 0.5; |
86 |
gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 12); |
87 |
add(instructions, gridBagConstraints); |
88 |
|
89 |
charsetComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[Loading all character sets]" })); |
90 |
charsetComboBox.setEnabled(false); |
91 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
92 |
gridBagConstraints.gridx = 0; |
93 |
gridBagConstraints.gridy = 1; |
94 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
95 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; |
96 |
gridBagConstraints.weightx = 0.5; |
97 |
gridBagConstraints.insets = new java.awt.Insets(5, 12, 12, 12); |
98 |
add(charsetComboBox, gridBagConstraints); |
99 |
}// </editor-fold>//GEN-END:initComponents |
100 |
|
101 |
@Override |
102 |
public void addNotify() { |
103 |
super.addNotify(); |
104 |
RequestProcessor.getDefault().post(this); |
105 |
} |
106 |
|
107 |
// Variables declaration - do not modify//GEN-BEGIN:variables |
108 |
private javax.swing.JComboBox charsetComboBox; |
109 |
private javax.swing.JLabel instructions; |
110 |
// End of variables declaration//GEN-END:variables |
111 |
|
112 |
@Override |
113 |
public void run() { |
114 |
if (!EventQueue.isDispatchThread()) { |
115 |
try { |
116 |
Entry use = null; |
117 |
Charset def = FileEncodingQuery.getDefaultEncoding(); |
118 |
if (!EventQueue.isDispatchThread()) { |
119 |
SortedMap<String, Charset> m = Charset.availableCharsets(); |
120 |
for (Map.Entry<String, Charset> e : m.entrySet()) { |
121 |
Entry entry = new Entry(e.getKey(), e.getValue()); |
122 |
if (def.equals(e.getValue())) { |
123 |
use = entry; |
124 |
} |
125 |
mdl.addElement(entry); |
126 |
} |
127 |
} |
128 |
if (use != null) { |
129 |
mdl.setSelectedItem(use); |
130 |
} |
131 |
} finally { |
132 |
EventQueue.invokeLater(this); |
133 |
} |
134 |
} else { |
135 |
charsetComboBox.setModel(mdl); |
136 |
charsetComboBox.setEnabled(true); |
137 |
Cursor cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); |
138 |
setCursor(cursor); |
139 |
instructions.setCursor(cursor); |
140 |
charsetComboBox.setCursor(cursor); |
141 |
if (descriptor != null) { |
142 |
descriptor.setValid(false); |
143 |
} |
144 |
} |
145 |
} |
146 |
|
147 |
private static final class Entry { |
148 |
|
149 |
String nm; |
150 |
Charset charset; |
151 |
|
152 |
public Entry(String nm, Charset charset) { |
153 |
this.nm = nm; |
154 |
this.charset = charset; |
155 |
} |
156 |
|
157 |
@Override |
158 |
public String toString() { |
159 |
return charset.displayName(); |
160 |
} |
161 |
} |
162 |
} |