| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.geotoolkit.gui.swing.image; |
| 19 | |
|
| 20 | |
import java.util.List; |
| 21 | |
import java.util.Arrays; |
| 22 | |
import java.util.Locale; |
| 23 | |
import java.util.Comparator; |
| 24 | |
import java.util.Collections; |
| 25 | |
import java.util.ResourceBundle; |
| 26 | |
import java.util.MissingResourceException; |
| 27 | |
import java.util.logging.Level; |
| 28 | |
|
| 29 | |
import java.awt.Component; |
| 30 | |
import java.awt.BorderLayout; |
| 31 | |
import javax.swing.Icon; |
| 32 | |
import javax.swing.Box; |
| 33 | |
import javax.swing.JTree; |
| 34 | |
import javax.swing.JLabel; |
| 35 | |
import javax.swing.JComponent; |
| 36 | |
import javax.swing.JScrollPane; |
| 37 | |
import javax.swing.BorderFactory; |
| 38 | |
import javax.swing.tree.TreePath; |
| 39 | |
import javax.swing.tree.TreeModel; |
| 40 | |
import javax.swing.tree.TreeSelectionModel; |
| 41 | |
import javax.swing.tree.DefaultTreeCellRenderer; |
| 42 | |
import javax.swing.event.TreeSelectionEvent; |
| 43 | |
import javax.swing.event.TreeSelectionListener; |
| 44 | |
import java.awt.image.renderable.ParameterBlock; |
| 45 | |
|
| 46 | |
import javax.media.jai.JAI; |
| 47 | |
import javax.media.jai.OperationRegistry; |
| 48 | |
import javax.media.jai.OperationDescriptor; |
| 49 | |
import javax.media.jai.ParameterListDescriptor; |
| 50 | |
import javax.media.jai.RegistryElementDescriptor; |
| 51 | |
|
| 52 | |
import org.geotoolkit.util.converter.Classes; |
| 53 | |
import org.geotoolkit.gui.swing.IconFactory; |
| 54 | |
import org.geotoolkit.gui.swing.tree.TreeNode; |
| 55 | |
import org.geotoolkit.gui.swing.tree.NamedTreeNode; |
| 56 | |
import org.geotoolkit.gui.swing.tree.DefaultTreeModel; |
| 57 | |
import org.geotoolkit.gui.swing.tree.DefaultMutableTreeNode; |
| 58 | |
import org.geotoolkit.resources.Vocabulary; |
| 59 | |
import org.geotoolkit.util.logging.Logging; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
@SuppressWarnings("serial") |
| 97 | 0 | public class RegisteredOperationBrowser extends JComponent { |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 1 | private final JLabel description = new JLabel(" "); |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | 1 | private final JLabel version = new JLabel(" "); |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public RegisteredOperationBrowser() { |
| 112 | 1 | this(getTree()); |
| 113 | 1 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public RegisteredOperationBrowser(final OperationRegistry registry) { |
| 121 | 0 | this(getTree(registry, getDefaultLocale())); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | 1 | private RegisteredOperationBrowser(final TreeModel model) { |
| 130 | 1 | setLayout(new BorderLayout()); |
| 131 | 1 | final JTree tree = new JTree(model); |
| 132 | 1 | tree.setBorder(BorderFactory.createEmptyBorder(6, 6, 0, 0)); |
| 133 | 1 | add(new JScrollPane(tree), BorderLayout.CENTER); |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 1 | final Box labels = Box.createVerticalBox(); |
| 138 | 1 | labels.add(description); |
| 139 | 1 | labels.add(version); |
| 140 | 1 | labels.setBorder(BorderFactory.createEmptyBorder(3, 6, 3, 0)); |
| 141 | 1 | add(labels, BorderLayout.SOUTH); |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | 1 | tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
| 146 | 1 | tree.addTreeSelectionListener(new TreeSelectionListener() { |
| 147 | |
@Override public void valueChanged(final TreeSelectionEvent event) { |
| 148 | 0 | selected(event.getNewLeadSelectionPath()); |
| 149 | 0 | } |
| 150 | |
}); |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | 1 | tree.setCellRenderer(new CellRenderer()); |
| 155 | 1 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private void selected(final TreePath path) { |
| 164 | 0 | if (path != null) { |
| 165 | 0 | for (int i=path.getPathCount(); --i>=0;) { |
| 166 | 0 | final Object component = path.getPathComponent(i); |
| 167 | 0 | Object candidate = component; |
| 168 | 0 | if (candidate instanceof TreeNode) { |
| 169 | 0 | candidate = ((TreeNode) candidate).getUserObject(); |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
} |
| 175 | 0 | if (candidate instanceof OperationDescriptor) { |
| 176 | 0 | int index = -1; |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 0 | if (component instanceof javax.swing.tree.TreeNode) { |
| 182 | 0 | final javax.swing.tree.TreeNode node = (javax.swing.tree.TreeNode)component; |
| 183 | 0 | final Object leaf = path.getLastPathComponent(); |
| 184 | 0 | for (index=node.getChildCount(); --index>=0;) { |
| 185 | 0 | final javax.swing.tree.TreeNode param = node.getChildAt(index); |
| 186 | 0 | if (param==leaf && !param.getAllowsChildren()) { |
| 187 | 0 | break; |
| 188 | |
} |
| 189 | 0 | } |
| 190 | |
} |
| 191 | 0 | selected((OperationDescriptor) candidate, index); |
| 192 | 0 | return; |
| 193 | |
} |
| 194 | 0 | } |
| 195 | |
} |
| 196 | 0 | selected(null, -1); |
| 197 | 0 | } |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
protected void selected(final OperationDescriptor operation, final int param) { |
| 209 | 0 | String description = " "; |
| 210 | 0 | String version = " "; |
| 211 | 0 | if (operation != null) { |
| 212 | |
final String key; |
| 213 | 0 | final Locale locale = getLocale(); |
| 214 | 0 | final ResourceBundle resources = operation.getResourceBundle(locale); |
| 215 | 0 | if (param >= 0) { |
| 216 | 0 | key = "arg" + param + "Desc"; |
| 217 | |
} else { |
| 218 | 0 | key = "Description"; |
| 219 | |
} |
| 220 | |
try { |
| 221 | 0 | description = resources.getString(key); |
| 222 | 0 | version = Vocabulary.getResources(locale).getString(Vocabulary.Keys.VERSION_$1, |
| 223 | |
resources.getString("Version")) + ", " + resources.getString("Vendor"); |
| 224 | 0 | } catch (MissingResourceException exception) { |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | 0 | Logging.getLogger(RegisteredOperationBrowser.class).log(Level.FINER, |
| 231 | |
exception.getLocalizedMessage(), exception); |
| 232 | 0 | } |
| 233 | |
} |
| 234 | 0 | this.description.setText(description); |
| 235 | 0 | this.version .setText(version ); |
| 236 | 0 | } |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public static TreeModel getTree() { |
| 245 | 1 | return getTree(JAI.getDefaultInstance().getOperationRegistry(), getDefaultLocale()); |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
public static TreeModel getTree(final OperationRegistry registry, final Locale locale) { |
| 261 | 1 | final Vocabulary resources = Vocabulary.getResources(locale); |
| 262 | 1 | final DefaultMutableTreeNode root = new DefaultMutableTreeNode( |
| 263 | |
resources.getString(Vocabulary.Keys.OPERATIONS)); |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | 1 | final String[] modes = registry.getRegistryModes(); |
| 269 | 1 | Arrays.sort(modes); |
| 270 | 8 | for (int i=0; i<modes.length; i++) { |
| 271 | 7 | final String mode = modes[i]; |
| 272 | 7 | final DefaultMutableTreeNode modeNode = new DefaultMutableTreeNode(mode); |
| 273 | |
@SuppressWarnings("unchecked") |
| 274 | 7 | final List<RegistryElementDescriptor> descriptors = registry.getDescriptors(mode); |
| 275 | 950 | Collections.sort(descriptors, new Comparator<RegistryElementDescriptor>() { |
| 276 | |
@Override public int compare(final RegistryElementDescriptor desc1, |
| 277 | |
final RegistryElementDescriptor desc2) |
| 278 | |
{ |
| 279 | 943 | return desc1.getName().compareTo(desc2.getName()); |
| 280 | |
} |
| 281 | |
}); |
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | 7 | for (final RegistryElementDescriptor descriptor : descriptors) { |
| 286 | |
final DefaultMutableTreeNode descriptorNode; |
| 287 | |
final ParameterListDescriptor param; |
| 288 | 179 | descriptorNode = new NamedTreeNode(getName(descriptor, locale), descriptor); |
| 289 | 179 | param = descriptor.getParameterListDescriptor(mode); |
| 290 | 179 | if (param != null) { |
| 291 | 177 | final String[] names = param.getParamNames(); |
| 292 | 177 | if (names != null) { |
| 293 | |
|
| 294 | 483 | for (int j=0; j<names.length; j++) { |
| 295 | |
|
| 296 | |
|
| 297 | 364 | descriptorNode.add(new DefaultMutableTreeNode(names[j], false)); |
| 298 | |
} |
| 299 | |
} |
| 300 | |
} |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | 179 | final String operationName = descriptor.getName(); |
| 305 | |
@SuppressWarnings("unchecked") |
| 306 | 179 | final List<String> products = registry.getOrderedProductList(mode, operationName); |
| 307 | 179 | if (products != null) { |
| 308 | |
final DefaultMutableTreeNode productsNode; |
| 309 | 113 | productsNode = new DefaultMutableTreeNode( |
| 310 | |
resources.getString(Vocabulary.Keys.IMPLEMENTATIONS)); |
| 311 | 113 | for (final String product : products) { |
| 312 | |
final DefaultMutableTreeNode productNode; |
| 313 | 113 | productNode = new DefaultMutableTreeNode(product); |
| 314 | 113 | final List<?> factories = registry.getOrderedFactoryList(mode, operationName, product); |
| 315 | 113 | if (factories != null) { |
| 316 | 113 | for (final Object factory : factories) { |
| 317 | 171 | productNode.add(new NamedTreeNode( |
| 318 | |
Classes.getShortClassName(factory), factory, false)); |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
} |
| 323 | |
} |
| 324 | 113 | productsNode.add(productNode); |
| 325 | 113 | } |
| 326 | 113 | descriptorNode.add(productsNode); |
| 327 | |
} |
| 328 | 179 | modeNode.add(descriptorNode); |
| 329 | 179 | } |
| 330 | 7 | root.add(modeNode); |
| 331 | |
} |
| 332 | 1 | return new DefaultTreeModel(root, true); |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
private static String getName(final RegistryElementDescriptor descriptor, final Locale locale) { |
| 342 | 179 | if (descriptor instanceof OperationDescriptor) { |
| 343 | 171 | ResourceBundle resources = ((OperationDescriptor)descriptor).getResourceBundle(locale); |
| 344 | 171 | if (resources != null) try { |
| 345 | 171 | return resources.getString("LocalName"); |
| 346 | 0 | } catch (MissingResourceException exception) { |
| 347 | |
|
| 348 | |
|
| 349 | |
} |
| 350 | |
} |
| 351 | 8 | return descriptor.getName(); |
| 352 | |
} |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
@SuppressWarnings("serial") |
| 364 | 1 | private static final class CellRenderer extends DefaultTreeCellRenderer { |
| 365 | |
|
| 366 | |
private final Icon open, closed; |
| 367 | |
|
| 368 | |
|
| 369 | |
private static Icon operation; |
| 370 | |
|
| 371 | |
|
| 372 | |
private static Icon parameter; |
| 373 | |
|
| 374 | |
|
| 375 | |
private static Icon implementation; |
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | 1 | private CellRenderer() { |
| 381 | 1 | open = getDefaultOpenIcon(); |
| 382 | 1 | closed = getDefaultClosedIcon(); |
| 383 | 1 | if (operation == null) { |
| 384 | 1 | final IconFactory icons = IconFactory.DEFAULT; |
| 385 | 1 | operation = icons.getIcon("crystalProject/16/apps/background.png"); |
| 386 | 1 | parameter = icons.getIcon("toolbarButtonGraphics/general/Preferences16.gif"); |
| 387 | 1 | implementation = icons.getIcon("crystalProject/16/actions/run.png"); |
| 388 | |
} |
| 389 | 1 | } |
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
@Override |
| 395 | |
public Component getTreeCellRendererComponent(final JTree tree, final Object value, |
| 396 | |
final boolean selelected, final boolean expanded, |
| 397 | |
final boolean leaf, final int row, final boolean hasFocus) |
| 398 | |
{ |
| 399 | |
final boolean isOp; |
| 400 | 16 | isOp = ((TreeNode) value).getUserObject() instanceof RegistryElementDescriptor; |
| 401 | 16 | Icon icon = isOp ? operation : open; |
| 402 | 16 | if (icon != null) { |
| 403 | 16 | setOpenIcon(icon); |
| 404 | |
} |
| 405 | 16 | icon = isOp ? operation : closed; |
| 406 | 16 | if (icon != null) { |
| 407 | 16 | setClosedIcon(icon); |
| 408 | |
} |
| 409 | 16 | icon = (value instanceof NamedTreeNode ? implementation : parameter); |
| 410 | 16 | if (icon != null) { |
| 411 | 16 | setLeafIcon(icon); |
| 412 | |
} |
| 413 | 16 | return super.getTreeCellRendererComponent(tree, value, selected, expanded, |
| 414 | |
leaf, row, hasFocus); |
| 415 | |
} |
| 416 | |
} |
| 417 | |
} |