You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by js...@apache.org on 2017/09/08 12:39:01 UTC
svn commit: r1807741 - in /sis/branches/JDK8/application/sis-javafx: ./
src/main/java/org/apache/sis/gui/dataset/
src/main/java/org/apache/sis/internal/
src/main/java/org/apache/sis/internal/gui/
Author: jsorel
Date: Fri Sep 8 12:39:00 2017
New Revision: 1807741
URL: http://svn.apache.org/viewvc?rev=1807741&view=rev
Log:
JavaFX : new ResourceTree widget
Added:
sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/
sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ResourceTree.java
sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/
sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/
sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/FontGlyphs.java
Modified:
sis/branches/JDK8/application/sis-javafx/pom.xml
Modified: sis/branches/JDK8/application/sis-javafx/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-javafx/pom.xml?rev=1807741&r1=1807740&r2=1807741&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-javafx/pom.xml (original)
+++ sis/branches/JDK8/application/sis-javafx/pom.xml Fri Sep 8 12:39:00 2017
@@ -88,6 +88,11 @@ Client application for JavaFX.
<artifactId>sis-metadata</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.webjars</groupId>
+ <artifactId>material-design-icons</artifactId>
+ <version>3.0.1</version>
+ </dependency>
</dependencies>
</project>
Added: sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ResourceTree.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ResourceTree.java?rev=1807741&view=auto
==============================================================================
--- sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ResourceTree.java (added)
+++ sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ResourceTree.java Fri Sep 8 12:39:00 2017
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.gui.dataset;
+
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javafx.beans.property.SimpleObjectProperty;
+import javafx.beans.value.ObservableValue;
+import javafx.collections.ObservableList;
+import javafx.embed.swing.SwingFXUtils;
+import javafx.geometry.Pos;
+import javafx.scene.control.ContentDisplay;
+import javafx.scene.control.TreeItem;
+import javafx.scene.control.TreeTableCell;
+import javafx.scene.control.TreeTableColumn;
+import javafx.scene.control.TreeTableRow;
+import javafx.scene.control.TreeTableView;
+import javafx.scene.image.Image;
+import javafx.scene.image.ImageView;
+import javafx.scene.text.TextAlignment;
+import javafx.util.Callback;
+import org.apache.sis.internal.gui.FontGlyphs;
+import org.apache.sis.storage.Aggregate;
+import org.apache.sis.storage.DataStore;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.FeatureSet;
+import org.apache.sis.storage.Resource;
+import org.opengis.metadata.Identifier;
+import org.opengis.metadata.Metadata;
+import org.opengis.metadata.citation.Citation;
+import org.opengis.metadata.identification.Identification;
+
+/**
+ * Tree viewer displaying a {@link Resource} hierarchy.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since 0.8
+ * @module
+ */
+public class ResourceTree extends TreeTableView<Resource> {
+
+ private static final Image ICON_VECTOR = SwingFXUtils.toFXImage(FontGlyphs.createImage("\uE922",18,Color.GRAY),null);
+ private static final Image ICON_FOLDER = SwingFXUtils.toFXImage(FontGlyphs.createImage("\uE2C8",18,Color.GRAY),null);
+ private static final Image ICON_STORE = SwingFXUtils.toFXImage(FontGlyphs.createImage("\uE2C7",18,Color.GRAY),null);
+ private static final Image ICON_OTHER = SwingFXUtils.toFXImage(FontGlyphs.createImage("\uE24D",18,Color.GRAY),null);
+
+ public ResourceTree() {
+ getColumns().add(new ResourceNameColumn());
+ setColumnResizePolicy(CONSTRAINED_RESIZE_POLICY);
+ }
+
+ /**
+ * Get root {@link Resource} of this tree.
+ *
+ * @return root {@link Resource}, may be null
+ */
+ public Resource getResource() {
+ final TreeItem<Resource> root = getRoot();
+ return root==null ? null : root.getValue();
+ }
+
+ /**
+ * Set root {@link Resource} of this tree.
+ *
+ * @param resource can be null
+ */
+ public void setResource(Resource resource) {
+ if (resource==null) {
+ setRoot(null);
+ } else {
+ setRoot(new ResourceItem(resource));
+ }
+ }
+
+ /**
+ * Find an appropriate icon for given resource.
+ *
+ * @param resource resource to test
+ * @return Image icon
+ */
+ private static Image getTypeIcon(Resource resource){
+ if (resource instanceof FeatureSet) {
+ return ICON_VECTOR;
+ } else if (resource instanceof DataStore) {
+ return ICON_STORE;
+ } else if (resource instanceof Aggregate) {
+ return ICON_FOLDER;
+ } else {
+ //unspecific resource type
+ return ICON_OTHER;
+ }
+ }
+
+ private static String getIdentifier(Metadata metadata) {
+ final Collection<? extends Identification> identifications = metadata.getIdentificationInfo();
+ for (Identification identification : identifications) {
+ final Citation citation = identification.getCitation();
+ if (citation != null) {
+ for (Identifier identifier : citation.getIdentifiers()) {
+ return identifier.getCode();
+ }
+ }
+ }
+ return null;
+ }
+
+
+ private static class ResourceItem extends TreeItem<Resource> {
+
+ private final Resource resource;
+ private boolean isFirstTimeChildren = true;
+
+ public ResourceItem(Resource res) {
+ super(res);
+ this.resource = res;
+ }
+
+ public ObservableList<TreeItem<Resource>> getChildren() {
+ if (isFirstTimeChildren) {
+ isFirstTimeChildren = false;
+ super.getChildren().setAll(buildChildren());
+ }
+ return super.getChildren();
+ }
+
+ @Override
+ public boolean isLeaf() {
+ return !(resource instanceof Aggregate);
+ }
+
+ private List<TreeItem<Resource>> buildChildren() {
+ if (resource instanceof Aggregate) {
+ final List<TreeItem<Resource>> lst = new ArrayList<>();
+ try {
+ for (Resource res : ((Aggregate) resource).components()) {
+ lst.add(new ResourceItem(res));
+ }
+ } catch (DataStoreException ex) {
+ ex.printStackTrace();
+ }
+ return lst;
+ }
+ return Collections.EMPTY_LIST;
+ }
+
+ }
+
+ private static class ResourceNameColumn extends TreeTableColumn<Resource,String> {
+
+ public ResourceNameColumn() {
+ super("Resource");
+ setCellValueFactory(new Callback<CellDataFeatures<Resource, String>, javafx.beans.value.ObservableValue<java.lang.String>>() {
+ @Override
+ public ObservableValue<String> call(CellDataFeatures<Resource, String> param) {
+ try {
+ return new SimpleObjectProperty<>(getIdentifier(param.getValue().getValue().getMetadata()));
+ } catch (DataStoreException ex) {
+ return new SimpleObjectProperty<>(ex.getMessage());
+ }
+ }
+ });
+ setCellFactory((TreeTableColumn<Resource, String> param) -> new Cell());
+ setEditable(true);
+ setPrefWidth(200);
+ setMinWidth(120);
+ setResizable(true);
+ }
+
+ }
+
+ private static class Cell extends TreeTableCell<Resource, String> {
+
+ @Override
+ public void updateItem(String item, boolean empty) {
+ super.updateItem(item, empty);
+ setText(item);
+ setGraphic(null);
+ setContentDisplay(ContentDisplay.LEFT);
+ setAlignment(Pos.CENTER_LEFT);
+ setTextAlignment(TextAlignment.LEFT);
+ setWrapText(false);
+ if (empty) return;
+
+ final TreeTableRow<Resource> row = getTreeTableRow();
+ if (row == null) {
+ return;
+ }
+ final TreeItem<Resource> ti = row.getTreeItem();
+ if (ti == null) {
+ return;
+ }
+
+ final Resource resource = ti.getValue();
+ setGraphic(new ImageView(getTypeIcon(resource)));
+ }
+ }
+
+}
Added: sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/FontGlyphs.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/FontGlyphs.java?rev=1807741&view=auto
==============================================================================
--- sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/FontGlyphs.java (added)
+++ sis/branches/JDK8/application/sis-javafx/src/main/java/org/apache/sis/internal/gui/FontGlyphs.java Fri Sep 8 12:39:00 2017
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.gui;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics2D;
+import java.awt.Insets;
+import java.awt.LinearGradientPaint;
+import java.awt.RenderingHints;
+import java.awt.geom.RoundRectangle2D;
+import java.awt.image.BufferedImage;
+import java.io.InputStream;
+import org.apache.sis.util.ArgumentChecks;
+
+/**
+ * Internal image tool to generate icons for javafx widgets.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @version 0.8
+ * @since 0.8
+ * @module
+ */
+public final class FontGlyphs {
+
+ private static Font FONT;
+
+ static {
+ try {
+ InputStream is = FontGlyphs.class.getResourceAsStream("/META-INF/resources/webjars/material-design-icons/3.0.1/MaterialIcons-Regular.ttf");
+ FONT = Font.createFont(Font.TRUETYPE_FONT, is);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ System.err.println("Font not loaded. Using serif font.");
+ FONT = new Font("serif", Font.PLAIN, 24);
+ }
+ }
+
+ private FontGlyphs() {
+ }
+
+ /**
+ * Create a image icon from glyph code and color.
+ *
+ * @param text glyph codes
+ * @param size output image size
+ * @param textColor glyph color
+ * @return glyph image
+ */
+ public static BufferedImage createImage(String text, float size, Color textColor) {
+ return createImage(text, textColor, FONT.deriveFont(size), null, null, true, false);
+ }
+
+ private static BufferedImage createImage(String text, Color textColor, Font font, Color bgColor, Insets insets, final boolean squareWanted, final boolean removeLeading) {
+ ArgumentChecks.ensureNonEmpty("Text to draw", text);
+ ArgumentChecks.ensureNonNull("Font to use", text);
+ if (insets == null) {
+ insets = new Insets(0, 0, 0, 0);
+ }
+
+ final int border = 0;
+ BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D g = img.createGraphics();
+ final FontMetrics fm = g.getFontMetrics(font);
+ final int textSize = fm.stringWidth(text);
+
+ int width = textSize + border * 2 + insets.left + insets.right;
+ int height = fm.getHeight() + border * 2 + insets.top + insets.bottom;
+ if (removeLeading) {
+ height -= fm.getLeading();
+ }
+
+ // We want a square. We compute additional margin to draw icon and text in center of thee square.
+ final int additionalLeftInset;
+ final int additionalTopInset;
+ if (squareWanted) {
+ final int tmpWidth = width;
+ width = Math.max(width, height);
+ additionalLeftInset = (width - tmpWidth) / 2;
+
+ final int tmpHeight = height;
+ height = Math.max(width, height);
+ additionalTopInset = (height - tmpHeight) / 2;
+ } else {
+ additionalLeftInset = 0;
+ additionalTopInset = 0;
+ }
+
+ img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ g = img.createGraphics();
+ g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+ final RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, width - 1, img.getHeight() - 1, border, border);
+
+ if (bgColor != null) {
+ final Color brighter = new Color(
+ Math.min(255, bgColor.getRed() + 100),
+ Math.min(255, bgColor.getGreen() + 100),
+ Math.min(255, bgColor.getBlue() + 100));
+
+ final LinearGradientPaint gradiant = new LinearGradientPaint(0, 0, 0, height, new float[]{0, 1}, new Color[]{brighter, bgColor});
+
+ g.setPaint(gradiant);
+ g.fill(rect);
+ }
+
+ int x = border + insets.left + additionalLeftInset;
+
+ //draw text
+ if (textColor != null) {
+ g.setColor(textColor);
+ }
+ g.setFont(font);
+ g.drawString(text, x, fm.getAscent() + border + insets.top + additionalTopInset);
+
+ if (bgColor != null) {
+ //draw border
+ g.setColor(Color.BLACK);
+ g.draw(rect);
+ }
+
+ return img;
+ }
+
+}