diff options
author | Wim Jongman | 2018-03-01 15:12:39 +0000 |
---|---|---|
committer | Wim Jongman | 2018-03-05 13:54:27 +0000 |
commit | f25308346936ce81c106c66ef764d11fa8a39d35 (patch) | |
tree | 6e750e5e3eac830a732cf549c8aa322e3daa0485 /org.eclipse.tips.json | |
parent | 127308c8fb9aa359f0aab9672a0987b56b23ed32 (diff) | |
download | eclipse.platform.ua-f25308346936ce81c106c66ef764d11fa8a39d35.tar.gz eclipse.platform.ua-f25308346936ce81c106c66ef764d11fa8a39d35.tar.xz eclipse.platform.ua-f25308346936ce81c106c66ef764d11fa8a39d35.zip |
Bug 531786 - [Tips] Add Json providerI20180305-2000
Adds a Json provider that can loads tips from a remote or local Json
file.
Added about.html
Change-Id: I06c67c42bc0a7fc38af06f622af82305b7cbb169
Signed-off-by: Wim Jongman <wim.jongman@remainsoftware.com>
Diffstat (limited to 'org.eclipse.tips.json')
17 files changed, 756 insertions, 0 deletions
diff --git a/org.eclipse.tips.json/.classpath b/org.eclipse.tips.json/.classpath new file mode 100644 index 000000000..eca7bdba8 --- /dev/null +++ b/org.eclipse.tips.json/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/org.eclipse.tips.json/.gitignore b/org.eclipse.tips.json/.gitignore new file mode 100644 index 000000000..988e9b210 --- /dev/null +++ b/org.eclipse.tips.json/.gitignore @@ -0,0 +1,3 @@ +/target/ +/bin/ +/minimal-json-0.9.5-sources.jar diff --git a/org.eclipse.tips.json/.project b/org.eclipse.tips.json/.project new file mode 100644 index 000000000..f11932451 --- /dev/null +++ b/org.eclipse.tips.json/.project @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.tips.json</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/org.eclipse.tips.json/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.tips.json/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..0c68a61dc --- /dev/null +++ b/org.eclipse.tips.json/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/org.eclipse.tips.json/.settings/org.eclipse.jdt.ui.prefs b/org.eclipse.tips.json/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 000000000..088036331 --- /dev/null +++ b/org.eclipse.tips.json/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,59 @@ +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=true +sp_cleanup.insert_inferred_type_arguments=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=false +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_type_arguments=false +sp_cleanup.remove_trailing_whitespaces=false +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=true +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/org.eclipse.tips.json/META-INF/MANIFEST.MF b/org.eclipse.tips.json/META-INF/MANIFEST.MF new file mode 100644 index 000000000..c88a38f11 --- /dev/null +++ b/org.eclipse.tips.json/META-INF/MANIFEST.MF @@ -0,0 +1,14 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Tip of the Day Json Provider +Bundle-SymbolicName: org.eclipse.tips.json;singleton:=true +Bundle-Version: 0.1.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Require-Bundle: org.eclipse.core.runtime;bundle-version="3.13.0", + org.eclipse.jface;bundle-version="3.13.1", + org.eclipse.tips.core;bundle-version="0.1.0" +Export-Package: org.eclipse.tips.json, + org.eclipse.tips.json.internal;x-internal:=true +Bundle-ClassPath: . +Automatic-Module-Name: org.eclipse.tips.json +Import-Package: com.google.gson;version="[2.0.0,3.0.0)" diff --git a/org.eclipse.tips.json/about.html b/org.eclipse.tips.json/about.html new file mode 100644 index 000000000..6dc51a06a --- /dev/null +++ b/org.eclipse.tips.json/about.html @@ -0,0 +1,78 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> +<title>About</title> +</head> +<body lang="EN-US"> +<h2>About This Content</h2> + +<p>April 14, 2008</p> +<h3>License</h3> + +<p>The Eclipse Foundation makes available all content in this +plug-in ("Content"). Unless otherwise indicated below, the Content is +provided to you under the terms and conditions of the Eclipse Public +License Version 1.0 ("EPL"). A copy of the EPL is available at <a + href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>. +For purposes of the EPL, "Program" will mean the Content.</p> + +<p>If you did not receive this Content directly from the Eclipse +Foundation, the Content is being redistributed by another party +("Redistributor") and different terms and conditions may apply to your +use of any object code in the Content. Check the Redistributor's license +that was provided with the Content. If no such license exists, contact +the Redistributor. Unless otherwise indicated below, the terms and +conditions of the EPL still apply to any source code in the Content and +such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p> + +<h3>Third Party Content</h3> +<p>The Content includes items that have been sourced from third +parties as set out below. If you did not receive this Content directly +from the Eclipse Foundation, the following is provided for informational +purposes only, and you should look to the Redistributor's license for +terms and conditions of use.</p> + +<h4>W3C DTDs</h4> + +<p>The plug-in is accompanied by document type definitions (" +W3C DTDs") defined by the the W3C Extensible HyperText Markup +Language (XHTML) 1.1. XHTML is described at <a + href="http://www.w3.org/TR/xhtml11/">http://www.w3.org/TR/xhtml11/</a>.</p> +The W3C DTDs are the following files located in the plug-in: +<p> +<ul> + <li><strong>xhtml11.dtd</strong> obtained from <a + href="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" target="_blank">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd</a></li> + <li><strong>xhtml11-flat.dtd</strong> obtained from <a + href="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" target="_blank">http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd</a></li> + <li><strong>xhtml1-frameset.dtd</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" + target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd</a></li> + <li><strong>xhtml1-strict</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" + target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd</a></li> + <li><strong>xhtml1-transitional.dtd</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" + target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a></li> + <li><strong>xhtml-lat1.ent</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent" target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent</a></li> + <li><strong>xhtml-special.ent</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent" + target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</a></li> + <li><strong>xhtml-symbol.ent</strong> obtained from <a + href="http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent" + target="_blank">http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</a></li> +</ul> + +<p>As described in section 5.10 of the FAQ located at <a + href="http://www.w3.org/Consortium/Legal/IPR-FAQ-20000620.html#DTD" + target="_blank">http://www.w3.org/Consortium/Legal/IPR-FAQ-20000620.html#DTD</a>, +your use of the W3C DTDs is governed by the terms and conditions of the +W3C Software License a copy of which is included in the file <a + href="about_files/copyright-software-19980720.htm">about_files/copyright-software-19980720.htm</a> +and is also available at <a + href="http://www.w3.org/Consortium/Legal/copyright-software-19980720" + target="_blank">http://www.w3.org/Consortium/Legal/copyright-software-19980720</a>.</p> +</body> +</html>
\ No newline at end of file diff --git a/org.eclipse.tips.json/build.properties b/org.eclipse.tips.json/build.properties new file mode 100644 index 000000000..07f663ae2 --- /dev/null +++ b/org.eclipse.tips.json/build.properties @@ -0,0 +1,14 @@ +############################################################################### +# Copyright (c) 2018 Remain Software +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# Contributors: +# wim.jongman@remainsoftware.com - initial API and implementation +############################################################################### +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/org.eclipse.tips.json/pom.xml b/org.eclipse.tips.json/pom.xml new file mode 100644 index 000000000..6621bac52 --- /dev/null +++ b/org.eclipse.tips.json/pom.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- Copyright (c) 2018 Remain Software All rights reserved. This program + and the accompanying materials are made available under the terms of the + Eclipse Public License v1.0 which accompanies this distribution, and is available + at http://www.eclipse.org/legal/epl-v10.html Contributors: wim.jongman@remainsoftware.com + - initial API and implementation --> + +<project + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>eclipse.platform.ua</groupId> + <artifactId>eclipse.platform.ua</artifactId> + <version>4.8.0-SNAPSHOT</version> + </parent> + <groupId>org.eclipse.ui</groupId> + <artifactId>org.eclipse.tips.json</artifactId> + <version>0.1.0-SNAPSHOT</version> + <packaging>eclipse-plugin</packaging> +</project> diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/IJsonTip.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/IJsonTip.java new file mode 100644 index 000000000..119c03272 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/IJsonTip.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.json; + +import org.eclipse.tips.core.Tip; + +/** + * Decorator of {@link Tip} to mark that this it is constructed from a Json + * structure. + * + */ +public interface IJsonTip { + + /** + * Returns the JsonObject that describes this Tip. + * + * @return the JsonObject describing the tip. + */ + public String getJsonObject(); +}
\ No newline at end of file diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java new file mode 100644 index 000000000..3a303b37d --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/JsonTipProvider.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.json; + +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.text.ParseException; +import java.util.ArrayList; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.tips.core.Tip; +import org.eclipse.tips.core.TipImage; +import org.eclipse.tips.core.TipProvider; +import org.eclipse.tips.core.internal.LogUtil; +import org.eclipse.tips.json.internal.JsonConstants; +import org.eclipse.tips.json.internal.JsonHTMLTip; +import org.eclipse.tips.json.internal.JsonUrlTip; +import org.eclipse.tips.json.internal.Util; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * A special TipProvider that gets instantiated from a JSon file. + * + */ +@SuppressWarnings("restriction") +public abstract class JsonTipProvider extends TipProvider { + + private URL fJsonUrl; + private String fDescription; + private String fImage; + + /** + * A method to set the a url containing a JSon file that describes this tip + * provider. + * + * @param jsonUrl + * the uRL to the Json file describing the provider and tips + * @throws MalformedURLException + * in case of an incorrect URL + */ + public void setJsonUrl(String jsonUrl) throws MalformedURLException { + fJsonUrl = new URL(jsonUrl); + } + + @Override + public IStatus loadNewTips(IProgressMonitor monitor) { + SubMonitor subMonitor = SubMonitor.convert(monitor); + ArrayList<Tip> result = new ArrayList<>(); + try { + subMonitor.beginTask(getDescription() + " Loading Tips", -1); + JsonObject value = (JsonObject) new JsonParser().parse(new InputStreamReader(fJsonUrl.openStream())); + JsonObject provider = value.getAsJsonObject(JsonConstants.P_PROVIDER); + fDescription = Util.getValueOrDefault(provider, JsonConstants.P_DESCRIPTION, "not set"); + fImage = Util.getValueOrDefault(provider, JsonConstants.P_IMAGE, null); + setExpression(Util.getValueOrDefault(provider, JsonConstants.P_EXPRESSION, null)); + JsonArray tips = provider.getAsJsonArray(JsonConstants.P_TIPS); + subMonitor.beginTask(getDescription() + " Creating Tips", -1); + tips.forEach(parm -> result.add(createJsonTip(parm))); + } catch (Exception e) { + Status status = new Status(IStatus.ERROR, "org.eclipse.tips.json", e.getMessage(), e); + getManager().log(status); + return status; + } + setTips(result); + subMonitor.done(); + return Status.OK_STATUS; + } + + @Override + public TipImage getImage() { + if (fImage == null) { + return null; + } + return new TipImage(fImage); + + } + + @Override + public String getDescription() { + return fDescription; + } + + private Tip createJsonTip(JsonElement parm) { + JsonObject json = (JsonObject) parm; + try { + if (json.get(JsonConstants.T_URL) == null) { + return new JsonHTMLTip(getID(), json); + } else { + return new JsonUrlTip(getID(), json); + } + } catch (ParseException e) { + getManager().log(LogUtil.error(getClass(), e)); + throw new RuntimeException(e); + } + } + + @Override + public void dispose() { + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonConstants.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonConstants.java new file mode 100644 index 000000000..9e5df148e --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonConstants.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.json.internal; + +/** + * Parser constants for the Json TipProvider.Tip[] Structure + * + * <pre> + * { + * "provider": { + * "image": "data:image/png;base64,iVBORw0K ... y543==", + * "description": "TipProvider Description", + * "expression": "<eclipse core expression>", + * "tips": [ + * { + * "subject": "Tip Subject", + * "date": "YYYY-MM-DD", + * "image": "data:image/png;base64,iVBORw0KGgo ... CYII=", + * "ratio": 1.5, + * "maxHeight": 300, + * "maxWidth": 200, + * "url": "url or html, not both", + * "html": "html or url, not both" + * }] + * } + * } + * </pre> + */ +public class JsonConstants { + + /** + * Provider description field + */ + public static final String P_DESCRIPTION = "description"; + + /** + * Provider core expression field + */ + public static final String P_EXPRESSION = "expression"; + + /** + * Provider image field + */ + public static final String P_IMAGE = "image"; + + /** + * Provider provider field + */ + public static final String P_PROVIDER = "provider"; + + /** + * The provider tips array + */ + public static final String P_TIPS = "tips"; + + /** + * Tip date field + */ + public static final String T_DATE = "date"; + + /** + * Tip HTML + */ + public static final String T_HTML = "html"; + + /** + * Tip image field + */ + public static final String T_IMAGE = "image"; + + /** + * Tip image maximum height (int) + */ + public static final String T_MAXHEIGHT = "maxHeight"; + + /** + * Tip image maximum width (int) + */ + public static final String T_MAXWIDTH = "maxWidth"; + + /** + * Tip image ratio field (double) + */ + public static final String T_RATIO = "ratio"; + + /** + * Tip subject field + */ + public static final String T_SUBJECT = "subject"; + + /** + * Tip URL + */ + public static final String T_URL = "url"; +}
\ No newline at end of file diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonHTMLTip.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonHTMLTip.java new file mode 100644 index 000000000..96e0f2af6 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonHTMLTip.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.json.internal; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.eclipse.tips.core.IHtmlTip; +import org.eclipse.tips.core.Tip; +import org.eclipse.tips.core.TipImage; +import org.eclipse.tips.json.IJsonTip; + +import com.google.gson.JsonObject; + +/** + * Internal implementation for a Json generated {@link IHtmlTip}. + * + */ +public class JsonHTMLTip extends Tip implements IJsonTip, IHtmlTip { + + private String fSubject; + private Date fDate; + private String fHtml; + private TipImage fTipImage; + private String fJsonObject; + + /** + * Creates the tip out od the passed Json object. + * + * @param jsonObject + * the json object + * @param provider + * the provider + * @throws ParseException + * when the json object could not be parsed. + */ + public JsonHTMLTip(String providerId, JsonObject jsonObject) throws ParseException { + super(providerId); + fJsonObject = jsonObject.toString(); + fSubject = Util.getValueOrDefault(jsonObject, JsonConstants.T_SUBJECT, "Not set"); + fDate = getDate(jsonObject); + fHtml = Util.getValueOrDefault(jsonObject, JsonConstants.T_HTML, null); + String base64Img = Util.getValueOrDefault(jsonObject, JsonConstants.T_IMAGE, null); + if (base64Img != null) { + fTipImage = new TipImage(base64Img); + double ratio = Util.getValueOrDefault(jsonObject, JsonConstants.T_RATIO, 0d); + if (ratio > 0) { + fTipImage.setAspectRatio(ratio); + } + int max = Util.getValueOrDefault(jsonObject, JsonConstants.T_MAXWIDTH, 0); + if (max > 0) { + fTipImage.setMaxWidth(max); + } + max = Util.getValueOrDefault(jsonObject, JsonConstants.T_MAXHEIGHT, 0); + if (max > 0) { + fTipImage.setMaxHeight(max); + } + } + } + + private static Date getDate(JsonObject jsonObject) throws ParseException { + String date = Util.getValueOrDefault(jsonObject, JsonConstants.T_DATE, "1970-01-01"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.parse(date); + } + + @Override + public String getJsonObject() { + return fJsonObject; + } + + @Override + public Date getCreationDate() { + return fDate; + } + + @Override + public String getHTML() { + return fHtml; + } + + @Override + public TipImage getImage() { + return fTipImage; + } + + @Override + public String getSubject() { + return fSubject; + } +} diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonUrlTip.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonUrlTip.java new file mode 100644 index 000000000..399c2e126 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/JsonUrlTip.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +package org.eclipse.tips.json.internal; + +import java.net.MalformedURLException; +import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.eclipse.tips.core.IUrlTip; +import org.eclipse.tips.core.Tip; +import org.eclipse.tips.json.IJsonTip; + +import com.google.gson.JsonObject; + +/** + * Internal implementation for a Json generated {@link IUrlTip}. + */ +public class JsonUrlTip extends Tip implements IUrlTip, IJsonTip { + + private String fSubject; + private Date fDate; + private String fUrl; + private String fJsonObject; + + /** + * Creates the tip out of the passed Json object. + * + * @param jsonObject + * the json object + * @param provider + * the provider + * @throws ParseException + * when the json object could not be parsed. + */ + public JsonUrlTip(String providerId, JsonObject jsonObject) throws ParseException { + super(providerId); + fJsonObject = jsonObject.toString(); + fSubject = Util.getValueOrDefault(jsonObject, JsonConstants.T_SUBJECT, "Not set"); + fDate = getDate(jsonObject); + fUrl = Util.getValueOrDefault(jsonObject, JsonConstants.T_URL, null); + } + + private static Date getDate(JsonObject jsonObject) throws ParseException { + String date = Util.getValueOrDefault(jsonObject, JsonConstants.T_DATE, "1970-01-01"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.parse(date); + } + + @Override + public Date getCreationDate() { + return fDate; + } + + @Override + public URL getURL() { + if (fUrl == null) { + return null; + } + try { + return new URL(fUrl); + } catch (MalformedURLException e) { + // TODO shoud thrown exeception and calling code should log +// getProvider().getManager().log(LogUtil.error(getClass(), e)); + return null; + } + } + + @Override + public String getSubject() { + return fSubject; + } + + @Override + public String getJsonObject() { + return fJsonObject; + } +}
\ No newline at end of file diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java new file mode 100644 index 000000000..c66a89ed1 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/Util.java @@ -0,0 +1,63 @@ +package org.eclipse.tips.json.internal; + +import com.google.gson.JsonObject; + +public class Util { + + /** + * Parses the passed json or returns a default value. + * + * @param jsonObject + * @param element + * the value to return in case the jsonObject does not contain the + * specified value. + * @param defaultValue + * the value to return in case the jsonObject does not contain the + * specified value. + * @return the returned value + */ + public static String getValueOrDefault(JsonObject jsonObject, String element, String defaultValue) { + if (jsonObject.has(element)) { + return jsonObject.get(element).getAsString(); + } + return defaultValue; + } + + /** + * Parses the passed json or returns a default value. + * + * @param jsonObject + * @param element + * the value to return in case the jsonObject does not contain the + * specified value. + * @param defaultValue + * the value to return in case the jsonObject does not contain the + * specified value. + * @return the returned value + */ + public static int getValueOrDefault(JsonObject jsonObject, String element, int defaultValue) { + if (jsonObject.has(element)) { + return jsonObject.get(element).getAsInt(); + } + return defaultValue; + } + + /** + * Parses the passed json or returns a default value. + * + * @param jsonObject + * @param element + * the value to return in case the jsonObject does not contain the + * specified value. + * @param defaultValue + * the value to return in case the jsonObject does not contain the + * specified value. + * @return the returned value + */ + public static double getValueOrDefault(JsonObject jsonObject, String element, double defaultValue) { + if (jsonObject.has(element)) { + return jsonObject.get(element).getAsDouble(); + } + return defaultValue; + } +} diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/package-info.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/package-info.java new file mode 100644 index 000000000..c02078018 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/internal/package-info.java @@ -0,0 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +/** + * Internal implementations of the Json providers. + */ +package org.eclipse.tips.json.internal;
\ No newline at end of file diff --git a/org.eclipse.tips.json/src/org/eclipse/tips/json/package-info.java b/org.eclipse.tips.json/src/org/eclipse/tips/json/package-info.java new file mode 100644 index 000000000..e40371005 --- /dev/null +++ b/org.eclipse.tips.json/src/org/eclipse/tips/json/package-info.java @@ -0,0 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2018 Remain Software + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wim.jongman@remainsoftware.com - initial API and implementation + *******************************************************************************/ +/** + * Provides some reusable classes. + */ +package org.eclipse.tips.json;
\ No newline at end of file |