Hi Hannes, I attached to this mail a code I used to "Colorize" DXF files according to the values on Attribute "Color" (0, 1,2,3, etc, according to autocad parameter = "black, red, etc"). This code is not tested with the new OJ, but I belive it is still working. You can find probably what you need. Best regards Peppe
package it.geoarbores.openjump.cadtools.cad.plugins; import com.vividsolutions.jump.feature.FeatureCollectionWrapper; import com.vividsolutions.jump.feature.FeatureSchema; import com.vividsolutions.jump.workbench.JUMPWorkbench; import com.vividsolutions.jump.workbench.WorkbenchContext; import com.vividsolutions.jump.workbench.model.Layer; import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.EnableCheck; import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory; import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck; import com.vividsolutions.jump.workbench.plugin.PlugInContext; import com.vividsolutions.jump.workbench.ui.MenuNames; import com.vividsolutions.jump.workbench.ui.WorkbenchFrame; import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller; import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle; import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingStyle; import com.vividsolutions.jump.workbench.ui.renderer.style.LabelStyle; import java.awt.Color; import java.util.Map; import javax.swing.JComponent; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import org.openjump.core.apitools.LayerTools; public class ApplyCADStyleCOLOR extends AbstractPlugIn { private WorkbenchContext workbenchContext; public void initialize(PlugInContext context) throws Exception { WorkbenchContext workbenchContext = context.getWorkbenchContext(); FeatureInstaller featureInstaller = new FeatureInstaller( workbenchContext); JPopupMenu layerNamePopupMenu = context.getWorkbenchContext() .getWorkbench().getFrame().getLayerNamePopupMenu(); String CAD = I18NPlug.getI18N("ApplyCADStylePlugIn"); featureInstaller.addPopupMenuPlugin( layerNamePopupMenu, this, new String[] { MenuNames.STYLE, I18NPlug.getI18N("ApplyCADStylePlugIn") }, I18NPlug.getI18N("ApplyCADStylePlugIn.true.colors"), false, null, createEnableCheck(workbenchContext)); } public static EnableCheck createEnableCheck(WorkbenchContext workbenchContext) { EnableCheckFactory checkFactory = new EnableCheckFactory( workbenchContext); new MultiEnableCheck() .add(checkFactory .createWindowWithLayerViewPanelMustBeActiveCheck()) .add(checkFactory.createAtLeastNLayersMustBeEditableCheck(1)) .add(new EnableCheck() { public String check(JComponent component) { Layer layer = (Layer)LayerTools.getSelectedLayerable( ApplyCADStyleCOLOR.this, Layer.class); FeatureCollectionWrapper fcw = layer .getFeatureCollectionWrapper(); FeatureSchema schema = fcw.getFeatureSchema(); if ((schema.hasAttribute("COLOR")) && (schema.hasAttribute("TEXT"))) { return null; } return "Selected layer has no COLOR and TEXT attributes"; } }); } Map<Integer, String> attributeToLabelMap = null; public boolean execute(PlugInContext context) throws Exception { reportNothingToUndoYet(context); Layer layer = context.getSelectedLayer(0); FeatureCollectionWrapper fcw = layer.getFeatureCollectionWrapper(); FeatureSchema schema = fcw.getFeatureSchema(); if ((schema.hasAttribute("COLOR") & schema.hasAttribute("TEXT"))) { layer.getBasicStyle().setEnabled(true); layer.getBasicStyle().setLineColor(Color.DARK_GRAY); layer.getBasicStyle().setFillColor(Color.GRAY); ColorThemingStyle colorThemingStyle = ColorThemingStyle.get(layer); colorThemingStyle.setAttributeName("COLOR"); BasicStyle defStyle = new BasicStyle(); defStyle.setAlpha(105); colorThemingStyle.setDefaultStyle(defStyle); BasicStyle basStyle0 = new BasicStyle(); basStyle0.setFillColor(new Color(128, 128, 128)); basStyle0.setLineColor(Color.BLACK); basStyle0.setAlpha(255); basStyle0.setLineWidth(1); BasicStyle basStyle1 = new BasicStyle(); basStyle1.setFillColor(Color.RED); basStyle1.setLineColor(Color.RED); basStyle1.setAlpha(255); basStyle1.setLineWidth(1); BasicStyle basStyle2 = new BasicStyle(); basStyle2.setFillColor(Color.YELLOW); basStyle2.setLineColor(Color.YELLOW); basStyle2.setAlpha(255); basStyle2.setLineWidth(1); BasicStyle basStyle3 = new BasicStyle(); basStyle3.setFillColor(Color.GREEN); basStyle3.setLineColor(Color.GREEN); basStyle3.setAlpha(255); basStyle3.setLineWidth(1); BasicStyle basStyle4 = new BasicStyle(); basStyle4.setFillColor(Color.CYAN); basStyle4.setLineColor(Color.CYAN); basStyle4.setAlpha(255); basStyle4.setLineWidth(1); BasicStyle basStyle5 = new BasicStyle(); basStyle5.setFillColor(Color.BLUE); basStyle5.setLineColor(Color.BLUE); basStyle5.setAlpha(255); basStyle5.setLineWidth(1); BasicStyle basStyle6 = new BasicStyle(); basStyle6.setFillColor(Color.MAGENTA); basStyle6.setLineColor(Color.MAGENTA); basStyle6.setAlpha(255); basStyle6.setLineWidth(1); BasicStyle basStyle7 = new BasicStyle(); basStyle7.setFillColor(Color.WHITE); basStyle7.setLineColor(Color.BLACK); basStyle7.setAlpha(255); basStyle7.setLineWidth(1); BasicStyle basStyle8 = new BasicStyle(); basStyle8.setFillColor(new Color(65, 65, 65)); basStyle8.setLineColor(new Color(65, 65, 65)); basStyle8.setAlpha(255); basStyle8.setLineWidth(1); BasicStyle basStyle9 = new BasicStyle(); basStyle9.setFillColor(new Color(128, 128, 128)); basStyle9.setLineColor(new Color(128, 128, 128)); basStyle9.setAlpha(255); basStyle9.setLineWidth(1); BasicStyle basStyleL = new BasicStyle(); basStyle0.setFillColor(Color.DARK_GRAY); basStyle0.setLineColor(Color.GRAY); basStyleL.setAlpha(255); basStyleL.setLineWidth(1); Map<Object, BasicStyle> attributeToStyleMap = null; attributeToStyleMap.put(Integer.valueOf(0), basStyle0); attributeToStyleMap.put(Integer.valueOf(1), basStyle1); attributeToStyleMap.put(Integer.valueOf(2), basStyle2); attributeToStyleMap.put(Integer.valueOf(3), basStyle3); attributeToStyleMap.put(Integer.valueOf(4), basStyle4); attributeToStyleMap.put(Integer.valueOf(5), basStyle5); attributeToStyleMap.put(Integer.valueOf(6), basStyle6); attributeToStyleMap.put(Integer.valueOf(7), basStyle7); attributeToStyleMap.put(Integer.valueOf(8), basStyle8); attributeToStyleMap.put(Integer.valueOf(9), basStyle9); attributeToStyleMap.put(Integer.valueOf(256), basStyleL); colorThemingStyle .setAttributeValueToBasicStyleMap(attributeToStyleMap); Map<Object, String> attributeToLabelMap = null; attributeToLabelMap.put(Integer.valueOf(0), "Black"); attributeToLabelMap.put(Integer.valueOf(1), "Red"); attributeToLabelMap.put(Integer.valueOf(2), "Yellow"); attributeToLabelMap.put(Integer.valueOf(3), "Green"); attributeToLabelMap.put(Integer.valueOf(4), "Cyan"); attributeToLabelMap.put(Integer.valueOf(5), "Blue"); attributeToLabelMap.put(Integer.valueOf(6), "Magenta"); attributeToLabelMap.put(Integer.valueOf(7), "White"); attributeToLabelMap.put(Integer.valueOf(8), "Gray"); attributeToLabelMap.put(Integer.valueOf(9), "Light Gray"); attributeToLabelMap.put(Integer.valueOf(256), "Color by Layer"); colorThemingStyle.setAttributeValueToLabelMap(attributeToLabelMap); colorThemingStyle.setEnabled(true); layer.getBasicStyle().setEnabled(false); layer.fireAppearanceChanged(); layer.getLabelStyle().setEnabled(true); layer.getLabelStyle().setAttribute("TEXT"); layer.getLabelStyle().setColor(Color.black); layer.getLabelStyle().setAngleAttribute("TEXT_ROTATION"); layer.getLabelStyle().setHeightAttribute("TEXT_HEIGHT"); layer.getLabelStyle().setVerticalAlignment("ABOVE_LINE"); layer.getLabelStyle().setHorizontalPosition("RIGHT_SIDE"); layer.getLabelStyle().setScaling(true); layer.getLabelStyle().setHidingOverlappingLabels(false); layer.getLabelStyle().setHideAtScale(false); layer.getLabelStyle().setHorizontalAlignment(1); return true; } JOptionPane.showMessageDialog(null, I18NPlug.getI18N("Message.1")); return false; } } 2015-05-27 22:06 GMT+02:00 <edgar.sol...@web.de>: > hey Hannes, > > welcome to OJ :).. you probably do not need to actually hack a plugin for > that. most likely a bean script will suffice. check out > http://ojwiki.soldin.de/index.php?title=Scripting_with_BeanShell > > you are probably missing a repaint as in this example > > http://ojwiki.soldin.de/index.php?title=Beanshell:Changing_linewidth_of_selected_layers > > ..ede > > On 27.05.2015 14:59, Johannes Kröger wrote: > > Hi everyone, > > > > I am using Version 1.8.0, rev 4164. > > > > In the code of a plugin I am writing I defined a FeatureDataset with an > > attribute "number" with integer values from 0 to 2. I added that FD to > > a layer. Now I want to style the layer by that attribute. From various > > ancient mailing list threads and random guessing I ended up with this: > > > > layer.getBasicStyle().setEnabled(false); > > layer.addStyle(new ColorThemingStyle("number", > > CollectionUtil.createMap( > > new Object[]{ > > 0, new BasicStyle(Color.blue), > > 1, new BasicStyle(Color.orange), > > 2, new BasicStyle(Color.red)}), > > new BasicStyle(Color.green) //default > > ) > > ); > > layer.getStyle(ColorThemingStyle.class).setEnabled(true); > > layer.fireAppearanceChanged(); > > > > This should assign blue to features where the "number" is 0, orange to > > those with 1 etc. > > > > When I run this plugin within OpenJUMP, the features in the map are > > displayed in a grey color, not the colors I assigned. Once I open and > > directly "OK"-close the "Change Styles"-dialog of the layer, the style > > I defined in the code is used. So, what am I missing? > > > > I already tried appending this without success: > > context.getLayerManager().fireLayerChanged(layer, > LayerEventType.APPEARANCE_CHANGED); > > > > Is this even the right approach? I got it mostly from > > > http://lists.refractions.net/pipermail/jump-users/2004-August/001352.html > > > > If someone feels generous, I would also be highly interested in a basic > > example of using a ranged style. I got as far as knowing that I would > > need to use Range but have no idea how to do that or how to apply it. > > > > Thanks, Hannes > > > > > ------------------------------------------------------------------------------ > _______________________________________________ > Jump-pilot-devel mailing list > Jump-pilot-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel >
------------------------------------------------------------------------------
_______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel