Search in sources :

Example 1 with LabelWidget

use of org.csstudio.display.builder.model.widgets.LabelWidget in project org.csstudio.display.builder by kasemir.

the class ExampleModels method createModel.

/**
 * @return {@link DisplayModel}
 */
public static DisplayModel createModel() {
    final DisplayModel model = new DisplayModel();
    model.setPropertyValue(CommonWidgetProperties.propWidth, 1400);
    model.setPropertyValue(CommonWidgetProperties.propHeight, 20 * 50);
    for (int i = 0; i < 200; ++i) {
        final int x = 0 + (i / 20) * 132;
        final int y = 0 + (i % 20) * 50;
        final GroupWidget group = new GroupWidget();
        group.setPropertyValue(CommonWidgetProperties.propName, "Group " + i);
        group.setPropertyValue(CommonWidgetProperties.propX, x);
        group.setPropertyValue(CommonWidgetProperties.propY, y);
        group.setPropertyValue(CommonWidgetProperties.propWidth, 125);
        group.setPropertyValue(CommonWidgetProperties.propHeight, 53);
        final LabelWidget label = new LabelWidget();
        label.setPropertyValue(CommonWidgetProperties.propName, "Label " + i);
        label.setPropertyValue(CommonWidgetProperties.propX, 0);
        label.setPropertyValue(CommonWidgetProperties.propY, 4);
        label.setPropertyValue(CommonWidgetProperties.propWidth, 15);
        label.setPropertyValue(CommonWidgetProperties.propHeight, 15);
        label.setPropertyValue(CommonWidgetProperties.propText, Integer.toString(i));
        group.runtimeChildren().addChild(label);
        // For SWT implementation, rect. is not 'transparent',
        // so needs to be behind text
        final RectangleWidget rect = new RectangleWidget();
        rect.setPropertyValue(CommonWidgetProperties.propName, "Rect " + i);
        rect.setPropertyValue(CommonWidgetProperties.propX, 10);
        rect.setPropertyValue(CommonWidgetProperties.propY, 0);
        rect.setPropertyValue(CommonWidgetProperties.propWidth, 80);
        rect.setPropertyValue(CommonWidgetProperties.propHeight, 19);
        rect.setPropertyValue(CommonWidgetProperties.propScripts, Arrays.asList(new ScriptInfo("../org.csstudio.display.builder.runtime.test/examples/fudge_width.py", true, new ScriptPV("noise"))));
        group.runtimeChildren().addChild(rect);
        final TextUpdateWidget text = new TextUpdateWidget();
        text.setPropertyValue(CommonWidgetProperties.propName, "Text " + i);
        text.setPropertyValue(CommonWidgetProperties.propX, 30);
        text.setPropertyValue(CommonWidgetProperties.propY, 4);
        text.setPropertyValue(CommonWidgetProperties.propWidth, 45);
        text.setPropertyValue(CommonWidgetProperties.propHeight, 15);
        text.setPropertyValue(CommonWidgetProperties.propPVName, "ramp");
        group.runtimeChildren().addChild(text);
        model.runtimeChildren().addChild(group);
    }
    return model;
}
Also used : RectangleWidget(org.csstudio.display.builder.model.widgets.RectangleWidget) DisplayModel(org.csstudio.display.builder.model.DisplayModel) ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) ScriptPV(org.csstudio.display.builder.model.properties.ScriptPV) TextUpdateWidget(org.csstudio.display.builder.model.widgets.TextUpdateWidget)

Example 2 with LabelWidget

use of org.csstudio.display.builder.model.widgets.LabelWidget in project org.csstudio.display.builder by kasemir.

the class EmbeddedDisplayRepresentationUtil method createErrorModel.

/**
 * @param message Error message
 *  @return DisplayModel that shows the message
 */
private static DisplayModel createErrorModel(final Widget model_widget, final String message) {
    final LabelWidget info = new LabelWidget();
    info.propText().setValue(message);
    info.propForegroundColor().setValue(WidgetColorService.getColor(NamedWidgetColors.ALARM_DISCONNECTED));
    // Size a little smaller than the widget to fill but not require scrollbars
    final int wid = model_widget.propWidth().getValue() - 2;
    final int hei = model_widget.propHeight().getValue() - 2;
    info.propWidth().setValue(wid);
    info.propHeight().setValue(hei);
    final DisplayModel error_model = new DisplayModel();
    error_model.propWidth().setValue(wid);
    error_model.propHeight().setValue(hei);
    error_model.runtimeChildren().addChild(info);
    error_model.setUserData(DisplayModel.USER_DATA_EMBEDDING_WIDGET, model_widget);
    return error_model;
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget)

Example 3 with LabelWidget

use of org.csstudio.display.builder.model.widgets.LabelWidget in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method installWidgetsFromURL.

private static void installWidgetsFromURL(final DragEvent event, final List<Widget> widgets, final List<Runnable> updates) {
    final String choice;
    final Dragboard db = event.getDragboard();
    String url = db.getUrl();
    // Fix URL, which on linux can contain the file name twice:
    // "http://some/path/to/file.xyz\nfile.xyz"
    int sep = url.indexOf('\n');
    if (sep > 0) {
        url = url.substring(0, sep);
    }
    // Check URL's extension
    sep = url.lastIndexOf('.');
    final String ext = sep > 0 ? url.substring(1 + sep).toUpperCase() : null;
    if (EMBEDDED_FILE_EXTENSIONS.contains(ext)) {
        choice = EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName();
    } else if (IMAGE_FILE_EXTENSIONS.contains(ext)) {
        choice = PictureWidget.WIDGET_DESCRIPTOR.getName();
    } else {
        // Prompt user
        final List<String> choices = Arrays.asList(LabelWidget.WIDGET_DESCRIPTOR.getName(), EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName(), PictureWidget.WIDGET_DESCRIPTOR.getName(), WebBrowserWidget.WIDGET_DESCRIPTOR.getName());
        final ChoiceDialog<String> dialog = new ChoiceDialog<>(choices.get(3), choices);
        // Position dialog
        dialog.setX(event.getScreenX());
        dialog.setY(event.getScreenY());
        dialog.setTitle(Messages.WT_FromURL_dialog_title);
        dialog.setHeaderText(NLS.bind(Messages.WT_FromURL_dialog_headerFMT, reduceString(url)));
        dialog.setContentText(Messages.WT_FromURL_dialog_content);
        final Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
            choice = result.get();
        } else {
            return;
        }
    }
    if (LabelWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating LabelWidget for {0}", url);
        final LabelWidget widget = (LabelWidget) LabelWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propText().setValue(url);
        widgets.add(widget);
    } else if (WebBrowserWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating WebBrowserWidget for {0}", url);
        final WebBrowserWidget widget = (WebBrowserWidget) WebBrowserWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propWidgetURL().setValue(url);
        widgets.add(widget);
    } else if (PictureWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating PictureWidget for {0}", url);
        final PictureWidget widget = (PictureWidget) PictureWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propFile().setValue(url);
        widgets.add(widget);
        updates.add(() -> updatePictureWidgetSize(widget));
    } else if (EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating EmbeddedDisplayWidget for {0}", url);
        EmbeddedDisplayWidget widget = (EmbeddedDisplayWidget) EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propFile().setValue(url);
        widgets.add(widget);
        updates.add(() -> updateEmbeddedDisplayWidget(widget));
    }
}
Also used : ChoiceDialog(javafx.scene.control.ChoiceDialog) Optional(java.util.Optional) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) List(java.util.List) ArrayList(java.util.ArrayList) WebBrowserWidget(org.csstudio.display.builder.model.widgets.WebBrowserWidget) Dragboard(javafx.scene.input.Dragboard)

Example 4 with LabelWidget

use of org.csstudio.display.builder.model.widgets.LabelWidget in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method installWidgetsFromString.

private static void installWidgetsFromString(final DragEvent event, final String text, final SelectedWidgetUITracker selection_tracker, final List<Widget> widgets) {
    // Consider each word a separate PV
    final String[] words = text.split("[ \n]+");
    final boolean multiple = words.length > 1;
    final List<WidgetDescriptor> descriptors = getPVWidgetDescriptors();
    final List<String> choices = new ArrayList<>(descriptors.size() + (multiple ? 1 : 0));
    final String format = multiple ? Messages.WT_FromString_multipleFMT : Messages.WT_FromString_singleFMT;
    // Always offer a single LabelWidget for the complete text
    final String defaultChoice = NLS.bind(Messages.WT_FromString_singleFMT, LabelWidget.WIDGET_DESCRIPTOR.getName());
    choices.add(defaultChoice);
    // When multiple words were dropped, offer multiple label widgets
    if (multiple) {
        choices.add(NLS.bind(Messages.WT_FromString_multipleFMT, LabelWidget.WIDGET_DESCRIPTOR.getName()));
    }
    choices.addAll(descriptors.stream().map(d -> NLS.bind(format, d.getName())).collect(Collectors.toList()));
    Collections.sort(choices);
    final ChoiceDialog<String> dialog = new ChoiceDialog<>(defaultChoice, choices);
    dialog.setX(event.getScreenX() - 100);
    dialog.setY(event.getScreenY() - 200);
    dialog.setTitle(Messages.WT_FromString_dialog_title);
    dialog.setHeaderText(NLS.bind(Messages.WT_FromString_dialog_headerFMT, reduceStrings(words)));
    dialog.setContentText(Messages.WT_FromString_dialog_content);
    final Optional<String> result = dialog.showAndWait();
    if (!result.isPresent()) {
        return;
    }
    final String choice = result.get();
    if (defaultChoice.equals(choice)) {
        logger.log(Level.FINE, "Dropped text: created LabelWidget [{0}].", text);
        // Not a valid XML. Instantiate a Label object.
        final LabelWidget widget = (LabelWidget) LabelWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propText().setValue(text);
        widgets.add(widget);
    } else {
        // Parse selection back into widget descriptor
        final MessageFormat msgf = new MessageFormat(format);
        final String descriptorName;
        try {
            descriptorName = msgf.parse(choice)[0].toString();
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Cannot parse selected widget type " + choice, ex);
            return;
        }
        WidgetDescriptor descriptor = null;
        if (LabelWidget.WIDGET_DESCRIPTOR.getName().equals(descriptorName)) {
            descriptor = LabelWidget.WIDGET_DESCRIPTOR;
        } else {
            descriptor = descriptors.stream().filter(d -> descriptorName.equals(d.getName())).findFirst().orElse(null);
        }
        if (descriptor == null) {
            logger.log(Level.SEVERE, "Cannot obtain widget for " + descriptorName);
            return;
        }
        for (String word : words) {
            final Widget widget = descriptor.createWidget();
            logger.log(Level.FINE, "Dropped text: created {0} [{1}].", new Object[] { widget.getClass().getSimpleName(), word });
            if (widget instanceof PVWidget) {
                ((PVWidget) widget).propPVName().setValue(word);
            } else if (widget instanceof LabelWidget) {
                ((LabelWidget) widget).propText().setValue(word);
            } else {
                logger.log(Level.WARNING, "Unexpected widget type [{0}].", widget.getClass().getSimpleName());
            }
            final int index = widgets.size();
            if (index > 0) {
                // Place widgets below each other
                final Widget previous = widgets.get(index - 1);
                int x = previous.propX().getValue();
                int y = previous.propY().getValue() + previous.propHeight().getValue();
                // Align (if enabled)
                final Point2D pos = selection_tracker.gridConstrain(x, y);
                widget.propX().setValue((int) pos.getX());
                widget.propY().setValue((int) pos.getY());
            }
            widgets.add(widget);
        }
    }
}
Also used : Color(java.awt.Color) ToolkitRepresentation(org.csstudio.display.builder.representation.ToolkitRepresentation) Arrays(java.util.Arrays) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) RenderingHints(java.awt.RenderingHints) ChoiceDialog(javafx.scene.control.ChoiceDialog) DragEvent(javafx.scene.input.DragEvent) ModelReader(org.csstudio.display.builder.model.persist.ModelReader) SelectedWidgetUITracker(org.csstudio.display.builder.editor.tracker.SelectedWidgetUITracker) PVWidget(org.csstudio.display.builder.model.widgets.PVWidget) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageIO(javax.imageio.ImageIO) Palette(org.csstudio.display.builder.editor.palette.Palette) Point2D(javafx.geometry.Point2D) WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) WidgetDescriptor(org.csstudio.display.builder.model.WidgetDescriptor) BufferedImage(java.awt.image.BufferedImage) NLS(org.eclipse.osgi.util.NLS) WidgetClassesService(org.csstudio.display.builder.model.persist.WidgetClassesService) Messages(org.csstudio.display.builder.editor.Messages) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) WidgetFactory(org.csstudio.display.builder.model.WidgetFactory) RTFEditorKit(javax.swing.text.rtf.RTFEditorKit) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) Document(javax.swing.text.Document) ClipboardContent(javafx.scene.input.ClipboardContent) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Plugin.logger(org.csstudio.display.builder.editor.Plugin.logger) MouseEvent(javafx.scene.input.MouseEvent) TransferMode(javafx.scene.input.TransferMode) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) Dragboard(javafx.scene.input.Dragboard) Graphics2D(java.awt.Graphics2D) DisplayEditor(org.csstudio.display.builder.editor.DisplayEditor) WebBrowserWidget(org.csstudio.display.builder.model.widgets.WebBrowserWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) Stroke(java.awt.Stroke) ModelWriter(org.csstudio.display.builder.model.persist.ModelWriter) ModelResourceUtil(org.csstudio.display.builder.model.util.ModelResourceUtil) ArrayWidgetProperty(org.csstudio.display.builder.model.ArrayWidgetProperty) Dimension2D(javafx.geometry.Dimension2D) SymbolRepresentation(org.csstudio.display.builder.representation.javafx.widgets.SymbolRepresentation) Node(javafx.scene.Node) WritableImage(javafx.scene.image.WritableImage) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) SymbolWidget(org.csstudio.display.builder.model.widgets.SymbolWidget) File(java.io.File) EditorUtil(org.csstudio.display.builder.editor.EditorUtil) Consumer(java.util.function.Consumer) SwingFXUtils(javafx.embed.swing.SwingFXUtils) Widget(org.csstudio.display.builder.model.Widget) Collections(java.util.Collections) Image(javafx.scene.image.Image) InputStream(java.io.InputStream) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) PVWidget(org.csstudio.display.builder.model.widgets.PVWidget) WebBrowserWidget(org.csstudio.display.builder.model.widgets.WebBrowserWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) SymbolWidget(org.csstudio.display.builder.model.widgets.SymbolWidget) Widget(org.csstudio.display.builder.model.Widget) PVWidget(org.csstudio.display.builder.model.widgets.PVWidget) WidgetDescriptor(org.csstudio.display.builder.model.WidgetDescriptor) ChoiceDialog(javafx.scene.control.ChoiceDialog) Point2D(javafx.geometry.Point2D) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget)

Example 5 with LabelWidget

use of org.csstudio.display.builder.model.widgets.LabelWidget in project org.csstudio.display.builder by kasemir.

the class ClassSupportUnitTest method testErrors.

@Test
public void testErrors() throws Exception {
    final AtomicReference<String> last_log_message = new AtomicReference<>();
    final Handler log_handler = new Handler() {

        @Override
        public void publish(final LogRecord record) {
            last_log_message.set(record.getMessage());
        }

        @Override
        public void flush() {
        // Ignore
        }

        @Override
        public void close() throws SecurityException {
        // Ignore
        }
    };
    logger.addHandler(log_handler);
    final WidgetClassSupport widget_classes = getExampleClasses();
    // Using an unknown widget class results in a warning
    Widget widget = new LabelWidget();
    widget.propClass().setValue("NonexistingClass");
    widget_classes.apply(widget);
    assertThat(last_log_message.get(), containsString("NonexistingClass"));
    // Unknown widget type also generates warning
    widget = new Widget("Bogus");
    widget_classes.apply(widget);
    assertThat(last_log_message.get().toLowerCase(), containsString("unknown widget type"));
    // Re-defining the same class
    assertThat(widget_classes.getWidgetClasses("label"), hasItem("TITLE"));
    final LabelWidget another = new LabelWidget();
    another.setPropertyValue("name", "TITLE");
    another.propFont().setValue(WidgetFontService.get(NamedWidgetFonts.DEFAULT_BOLD));
    another.propFont().useWidgetClass(true);
    widget_classes.registerClass(another);
    assertThat(last_log_message.get(), containsString("TITLE"));
    assertThat(last_log_message.get(), containsString("more than once"));
}
Also used : LogRecord(java.util.logging.LogRecord) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) Handler(java.util.logging.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)13 Test (org.junit.Test)8 DisplayModel (org.csstudio.display.builder.model.DisplayModel)6 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 ChoiceDialog (javafx.scene.control.ChoiceDialog)2 Dragboard (javafx.scene.input.Dragboard)2 Widget (org.csstudio.display.builder.model.Widget)2 ScriptPV (org.csstudio.display.builder.model.properties.ScriptPV)2 RuleInfo (org.csstudio.display.builder.model.rules.RuleInfo)2 EmbeddedDisplayWidget (org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget)2 PictureWidget (org.csstudio.display.builder.model.widgets.PictureWidget)2 WebBrowserWidget (org.csstudio.display.builder.model.widgets.WebBrowserWidget)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 RenderingHints (java.awt.RenderingHints)1