Changing FavIcon in Vaadin using pure Java alongside Springboot - java

I want to change the favicon of the website in Vaadin that is combined with Springboot. I have also opted to go the pure Java route within Vaadin (no html page).
I followed this guide, which explains that the icon I wish to use should be added in the src/main/webapp/icons folder where it will be automatically picked up, resized, etc.
I tried that to no avail, after which I found this thread and subsequently this one. The latter link especially explains that Spring-boot has its own directories and that the webapp folder should be avoided. I tried using the spring directories, but again to no avail.
My resources folder currently looks as such:
My MainView as such:
#Route
#PageTitle("My new title")
public class MainView extends VerticalLayout {
And my SpringBootApplication class as such:
#SpringBootApplication
public class MyVaadinApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyVaadinApplication.class, args);
}
}
The icon still remains the default spring leaf:
Where am I going wrong in trying to set the favicon?

Add icon.png to resources/META-INF/resources/icons/icon.png
Then:
If you are use progressive web app (#PWA annotation) it should already work
If you are use simple vaadin application you should to implements PageConfiguration and add link to your favicon.
#Route("favicon")
public class FaviconTest extends Div implements PageConfigurator {
#Override
public void configurePage(InitialPageSettings settings) {
HashMap<String, String> attributes = new HashMap<>();
attributes.put("rel", "shortcut icon");
attributes.put("type", "image/png");
settings.addLink("icons/icon.png", attributes);
}
}

Related

How to set the the main theme at build or deploy time?

I look for way in Vaadin Flow & Spring Boot to set the main theme at build time or via properties, but it is not working. Any ideas how it can be achieved?
Background: I have an application which I like to have the same code but deployed on different domains with different theme.
I tried to use a property from Spring Boot "application.properties" like "theme=my-vaadin-app", but this seems not to be supported. So any other way to set the theme at build or deploy time?
#SpringBootApplication
#Theme(value = "my-vaadin-app") // the usual way
#Theme(vaule = "${thene}") // NOT Working
public class MyVaadinApplication implements AppShellConfigurator {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
SpringApplication.run(MyVaadinApplication .class, args);
}
The theme is included in the optimized client-side bundle that is created as part of the production build.
If there are only small differences between your different theme variants, then you can include support for all of those variants in the same theme with CSS selectors for different classnames. You can then only change the classname at runtime while always using the same CSS content.
The other alternative is that you create separate builds for separate cases.

Java template for a project in Eclipse

Is it possible to make creation of (Java) file(s) in Eclipse easier/quicker.
I know there is that "Create new class wizard", but it is slow for my "special" needs...
I have a specific project in which I'm creating new classes often, but the structure for those classes is the same. Let say I want to create class A, so I want file A.java to be created as
class A {
public static void main(String[] args) {
}
static int solve() {
}
}
and it would be perfect that also ATest.java is created for this class, for example
class ATest {
#Test
int test1() {
Assert.assertEquals(0, A.solve());
}
}
or is there such plugin for Eclipse?
Yes, you can try using FastCode Plugin, where you can create new templates as per your requirement. As FastCode plugin supports custom templates, here is an example how to create the above class and test class together:
<template name="CREATE_NEW_CLASS">
<description>Used to Create class in the specified package.</description>
<allowed-file-names>*.*</allowed-file-names>
<first-template-item>package</first-template-item>
<second-template-item>none</second-template-item>
<additional-parameters>className</additional-parameters>
<template-body>
<![CDATA[
<fc:class type="class" name="${className}" package="${package.name}" project="${package.javaProject}">
public class ${className} {
public static void main(String[] args) {
}
static int solve() {
}
}
</fc:class>
<fc:class type="test" name="${className}Test" package="${package.name}" project="${package.javaProject}">
public class ${className}Test {
#Test
int test1(){
Assert.assertEquals(0, ${className}.solve());
}
}
</fc:class>
]]>
</template-body>
</template>
Once you add the template, you need to do import using import option in template preference page as explained in the document.
Yes, this is relatively simple to set up. Open the Project Properties and navigate to the Java Code Style > Code Templates section. Once there, check the box to enable project-specific settings. The generation template you want is under the Code part of the tree; you want Class Body. It is probably empty, but click the Edit... button to modify it.
Whatever you enter in the Edit dialog will be inserted between the class' brackets when using the New Class wizard.
There's no way I know of to automatically create another class (the Test in your case). But Eclipse has a JUnit wizard that makes doing so very easy. Just right-click on a class and choose New > Other... and then find Junit Test Case in the list. That wizard will guide you through creating the test class, including selecting the method(s) you want to test.
Note: these instructions set up the template for just the project or projects you select. You could also set up the same thing for your entire workspace Preferences, but doing so provides no way to share that configuration so that the same project checked out into another workspace will use it. I usually recommend making these kinds of settings on a per-project basis.
I'm not sure you need an IDE-specific template for that. When I was going through Project Euler, I had a setup like this:
public interface Problem {
public Object solve();
}
public class MyProblem implements Problem {
#Override
public Object solve() {
// do some stuff
return result;
}
}
Then in your (JUnit?) tests, you could use
Assert.assertEquals(expected, myProblemInstance.solve());
You can see my implementation here
If you do use an IDE template, you won't be able to use a generic solve() method, since it is not guaranteed that that class has that method. I would highly recommend using interfaces.

Create a JavaFX primary stage inside a normal Java application

I have a launcher and a JavaFX class. The launcher creates a class called JavaFXApplication1. The JavaFXApplication contains the whole JavaFX code (Just a little example in this case) and should setup a window with one primary stage.
The launcher has the static main entry point - but I read that JavaFX doesn't really use this entry point. This explains my console output (See the end of the post)
I don't know if this is possible (Launcher create a JavaFX window - the entry point is not in the presentation class itself) . I don't want to use a preloader (I think preloaders are just for heavy loads during startup), because the launcher represents the whole program as one object (Presentation, business and persistence - a 3 layer program). The entry point should be outside the presentation class (in this example in the launcher class)
The following example does work. But for me it is like a piece of "black magic"
Here is my code
Launcher:
package javafxapplication1;
public class Launcher
{
public static void main(String[] args)
{
System.out.println("main()");
// Do some stuff and then create the UI class
JavaFXApplication1 client = new JavaFXApplication1();
client.caller(args);
}
}
JavaFXApplication1:
package javafxapplication1;
import javafx.application.Application;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application
{
#Override
public void start(Stage primaryStage)
{
System.out.println("start()");
primaryStage.setTitle("I am a JavaFX app");
primaryStage.show();
}
public void caller(String[] args)
{
System.out.println("caller()");
launch(args);
}
/* We call the main function from the client
public static void main(String[] args)
{
launch(args);
}*/
}
And the output for the program is:
start()
Is there a way to create such an application ? Thank you
The answer to this problem is to create a java project and not a JavaFX project. After this you can add a JavaFX main class and write a method (call launch() ).
Maybe you have to add the compile-time libraries deploy.jar, javaws.jar, jfxrt.jar and plugin.jar from the /jdk_*/jre/lib directory
I written a post at
Running JavaFX Application instance in the main method of a class – MacDevign
Could it be what you looking for ?
The code is quite long hence it is better to refer to the post however the usage is simple. Take note that the init and stop method do not use launcher thread so use it with care.
The purpose is to run a dummy javafx application on your main method of your class for quick testing/experimenting purpose.
To use this, just add the following in the main method using lambda, or alternatively you can use anonymous inner class style.
// using the start method of Application class
Utility.launchApp((app, stage) -> {
// javafx code
}, arArgs);

Adding css resource available to all component in the application using wicket 1.5

I am using wicket 1.5.x and trying to load a css file which will be shared by all the pages,panels,forms. Right now when I statically add a css file (located in [app]/WebApps/style) directory in my BasePage and extend other pages.
Now if I want to use this css file for a panel,it does not get any class/id selector when I add a css class into that file for a panel .Neither the CSS file is attached to html head of BasePage. So Instead I wanted to use a global css file. I have tried to do like this:
In my Application class I did like this by calling the following function in init(),
private void mountResources(){
mountResource("/css/layout.css", Resources.CSS_BASE);
}
where my Resource class is,
public abstract class Resources {
public static final ResourceReference CSS_BASE
= new CssResourceReference(OrbitApplication.class, "resources/layout.css");
}
Here my css is in main/resource/ directory(maven structure). But the css is not loading. I have heared about HeaderContributor but did not find how to use in my wicket 1.5 appllication.
Any idea/snippet of how to do this? help appreciated.
Use
MyApp.init() {
super.init();
org.apache.wicket.Application.getHeaderContributorListenerCollection().add(new IHeaderContributor() {
public void renderHead(IHeaderResponse response) {
response.renderCssReference(Resources.CSS_BASE);
}
});
}
Why don't you read the responses to your questions in IRC ?

Eclipse JSP preview

Is there an Eclipse plugin or feature that allows previewing of JSP files? Ideally such a feature would be aware of Spring tags. It's a major pain to edit the JSP in Eclipse, then build and deploy to see the results.
I haven't seen any good plugin which will satisfy your requirement.
As an alternative you can put the jetty server's jar to your class path (I am using jetty-6.1.5.jar and jetty-util-6.1.5.jar) and write a class like the following.
package net.eduportal.jetty;
import javax.servlet.ServletContext;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.webapp.WebAppContext;
public class JettyRunner {
public static final int PORT = 8080;
public static final String BASE_URL = "http://localhost:" + PORT;
private static final JettyRunner _instance = new JettyRunner();
public static JettyRunner getInstance() {
return _instance;
}
// ///////////////////////////////////////////////////////////////
// Singleton
// /////////////
private Server server = null;
private WebAppContext wac = null;
private JettyRunner() {
}
public interface WebApplicationInitializer {
public void init(WebAppContext wac);
}
public ServletContext getServletContext() {
return wac.getServletContext();
}
public void start() throws Exception {
if (server == null) {
server = new Server(PORT);
server.setStopAtShutdown(true);
wac = new WebAppContext();
wac.setContextPath("/test");
wac.setResourceBase("war");
wac.setClassLoader(this.getClass().getClassLoader());
server.addHandler(wac);
server.start();
}
}
public void stop() throws Exception {
if (server != null) {
server.stop();
server = null;
}
}
public static void main(String argv[]) throws Exception {
JettyRunner.getInstance().start();
}
}
The above code assumes there is a folder called "war" in the class path which contains the same WEB-INF/* folders. When you run the code from eclipse the server will start and you can view the jsps by accessing the location localhost:8080/test/*
See http://jetty.mortbay.org/jetty5/tut/Server.html
You shouldn't have to rebuild at all to see the results.
The latest Enterprise version of eclipse actually does hot code replacement of JSPs. I add the web project to Tomcat (or Glassfish or JBoss...) and any change I make in a JSP is reflected after I refresh my browser window. Obviously, when I change a Java file, I need to restart Tomcat, but that only takes 2 seconds at most.
MyEclipse provides this plugin:
http://www.myeclipseide.com/module-htmlpages-display-pid-11.html
As to whether it will be Spring tag aware is another matter though...
JBoss Tools (http://jboss.org/tools) has a visual page editor that supports JSP, HTML and even JSF.
If a tag is not supported you can right click it and add a template for it OR you can extend the supported tags by implementing the extension points.
Examples of users extending the set of supported tags are http://relation.to/Bloggers/HowToCreateAVisualDocBookEditorIn10Minutes and http://planetjbpm.wordpress.com/2009/02/25/xforms-editor-with-jboss-vpe-and-some-jbpm/
There's the Oracle Workshop for WebLogic 10g R3 which gives you the closest thing to WYSIWYG JSP editing. Despite the fact that it comes from Oracle/BEA, it works with many app servers, not just WebLogic. It is the best tool I know for JSPs and it's free. I don't about Spring tags, but it can be customized to give design time representation of tags. I'm not sure if they support Eclipse 3.4 though.
There's also JBoss Developer Studio which has good JSP visual tools.

Categories

Resources