JMinor Application Framework

As simple as possible but not simpler

User Tools

Site Tools


documentation:tutorials:world

World: Application tutorial

Run the application

Web Start

World (Web Start)

You'll need to add http://jminor.no-ip.org to the Java exception site list.

Getdown

empdept-getdown.zip

Zip file containing the necessary files for running with Getdown.

Local JAR file

world.zip

Zip file containing a jar file along with an assortment of launch files for running the application without WebStart. Note that the remote configurations all assume the server is running on localhost.

Data

Domain model

File: org/jminor/framework/demos/world/domain/World.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.domain;
 
import org.jminor.framework.domain.Entities;
import org.jminor.framework.domain.Entity;
import org.jminor.framework.domain.Properties;
 
import java.sql.Types;
 
public final class World {
 
  public static final String T_CITY = "world.city";
  public static final String CITY_ID = "id";
  public static final String CITY_NAME = "name";
  public static final String CITY_COUNTRYCODE = "countrycode";
  public static final String CITY_COUNTRYCODE_FK = "countrycode_fk";
  public static final String CITY_DISTRICT = "district";
  public static final String CITY_POPULATION = "population";
 
  public static final String T_COUNTRY = "world.country";
  public static final String COUNTRY_CODE = "code";
  public static final String COUNTRY_NAME = "name";
  public static final String COUNTRY_CONTINENT = "continent";
  public static final String COUNTRY_REGION = "region";
  public static final String COUNTRY_SURFACEAREA = "surfacearea";
  public static final String COUNTRY_INDEPYEAR = "indepyear";
  public static final String COUNTRY_POPULATION = "population";
  public static final String COUNTRY_LIFEEXPECTANCY = "lifeexpectancy";
  public static final String COUNTRY_GNP = "gnp";
  public static final String COUNTRY_GNPOLD = "gnpold";
  public static final String COUNTRY_LOCALNAME = "localname";
  public static final String COUNTRY_GOVERNMENTFORM = "governmentform";
  public static final String COUNTRY_HEADOFSTATE = "headofstate";
  public static final String COUNTRY_CAPITAL = "capital";
  public static final String COUNTRY_CAPITAL_FK = "capital_fk";
  public static final String COUNTRY_CODE2 = "code2";
 
  static {
    //disable this default check so we can define a cyclical foreign key between country and city
    Entity.Definition.STRICT_FOREIGN_KEYS.set(false);
 
    Entities.define(T_COUNTRY,
            Properties.columnProperty(COUNTRY_CODE, Types.VARCHAR, "Country code")
                    .setPrimaryKeyIndex(0)
                    .setUpdatable(true)
                    .setMaxLength(3),
            Properties.columnProperty(COUNTRY_NAME, Types.VARCHAR, "Name")
                    .setNullable(false)
                    .setMaxLength(52),
            Properties.columnProperty(COUNTRY_CONTINENT, Types.VARCHAR, "Continent")
                    .setNullable(false)
                    .setMaxLength(20),
            Properties.columnProperty(COUNTRY_REGION, Types.VARCHAR, "Region")
                    .setNullable(false),
            Properties.columnProperty(COUNTRY_SURFACEAREA, Types.DOUBLE, "Surface area")
                    .setNullable(false)
                    .setMaximumFractionDigits(2),
            Properties.columnProperty(COUNTRY_INDEPYEAR, Types.INTEGER, "Indep. year")
                    .setMin(-5000).setMax(5000),
            Properties.columnProperty(COUNTRY_POPULATION, Types.INTEGER, "Population")
                    .setNullable(false),
            Properties.columnProperty(COUNTRY_LIFEEXPECTANCY, Types.DOUBLE, "Life expectancy")
                    .setMaximumFractionDigits(1)
                    .setMin(0).setMax(99),
            Properties.columnProperty(COUNTRY_GNP, Types.DOUBLE, "GNP")
                    .setMaximumFractionDigits(2),
            Properties.columnProperty(COUNTRY_GNPOLD, Types.DOUBLE, "GNP old")
                    .setMaximumFractionDigits(2),
            Properties.columnProperty(COUNTRY_LOCALNAME, Types.VARCHAR, "Local name")
                    .setNullable(false)
                    .setMaxLength(45),
            Properties.columnProperty(COUNTRY_GOVERNMENTFORM, Types.VARCHAR, "Government form")
                    .setNullable(false),
            Properties.columnProperty(COUNTRY_HEADOFSTATE, Types.VARCHAR, "Head of state"),
            Properties.foreignKeyProperty(COUNTRY_CAPITAL_FK, "Capital", T_CITY,
                    Properties.columnProperty(COUNTRY_CAPITAL)),
            Properties.columnProperty(COUNTRY_CODE2, Types.VARCHAR, "Code2")
                    .setNullable(false)
                    .setMaxLength(2))
            .setOrderByClause(COUNTRY_NAME)
            .setSearchPropertyIDs(COUNTRY_NAME)
            .setStringProvider(new Entities.StringProvider(COUNTRY_NAME))
            .setCaption("Country");
 
    Entities.define(T_CITY,
            Properties.columnProperty(CITY_ID)
                    .setPrimaryKeyIndex(0),
            Properties.columnProperty(CITY_NAME, Types.VARCHAR, "Name")
                    .setNullable(false)
                    .setMaxLength(35),
            Properties.foreignKeyProperty(CITY_COUNTRYCODE_FK, "Country", T_COUNTRY,
                    Properties.columnProperty(CITY_COUNTRYCODE, Types.VARCHAR))
                    .setNullable(false),
            Properties.columnProperty(CITY_DISTRICT, Types.VARCHAR, "District")
                    .setNullable(false)
                    .setMaxLength(20),
            Properties.columnProperty(CITY_POPULATION, Types.INTEGER, "Population")
                    .setNullable(false))
            .setKeyGenerator(Entities.sequenceKeyGenerator("world.city_seq"))
            .setOrderByClause(CITY_NAME)
            .setSearchPropertyIDs(CITY_NAME)
            .setStringProvider(new Entities.StringProvider(CITY_NAME))
            .setCaption("City");
  }
 
  public static final String T_COUNTRYLANGUAGE = "world.countrylanguage";
  public static final String COUNTRYLANGUAGE_COUNTRYCODE = "countrycode";
  public static final String COUNTRYLANGUAGE_COUNTRYCODE_FK = "countrycode_fk";
  public static final String COUNTRYLANGUAGE_LANGUAGE = "language";
  public static final String COUNTRYLANGUAGE_ISOFFICIAL = "isofficial";
  public static final String COUNTRYLANGUAGE_PERCENTAGE = "percentage";
 
  static {
    Entities.define(T_COUNTRYLANGUAGE,
            Properties.foreignKeyProperty(COUNTRYLANGUAGE_COUNTRYCODE_FK, "Country", T_COUNTRY,
                    Properties.columnProperty(COUNTRYLANGUAGE_COUNTRYCODE, Types.VARCHAR)
                            .setPrimaryKeyIndex(0)
                            .setUpdatable(true))
                    .setNullable(false),
            Properties.columnProperty(COUNTRYLANGUAGE_LANGUAGE, Types.VARCHAR, "Language")
                    .setPrimaryKeyIndex(1)
                    .setUpdatable(true),
            Properties.columnProperty(COUNTRYLANGUAGE_ISOFFICIAL, Types.BOOLEAN, "Is official")
                    .setColumnHasDefaultValue(true)
                    .setNullable(false),
            Properties.columnProperty(COUNTRYLANGUAGE_PERCENTAGE, Types.DOUBLE, "Percentage")
                    .setNullable(false)
                    .setMaximumFractionDigits(1)
                    .setMin(0).setMax(100))
            .setOrderByClause(COUNTRYLANGUAGE_LANGUAGE + ", " + COUNTRYLANGUAGE_PERCENTAGE + " desc")
            .setCaption("Language");
  }
}

Domain model unit test

File: org/jminor/framework/demos/world/domain/WorldTest.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.domain;
 
import org.jminor.common.User;
import org.jminor.common.db.exception.DatabaseException;
import org.jminor.framework.domain.Entities;
import org.jminor.framework.domain.Entity;
import org.jminor.framework.domain.testing.EntityTestUnit;
 
import org.junit.Test;
 
public final class WorldTest extends EntityTestUnit {
 
  private static final User UNIT_TEST_USER = new User(
          System.getProperty("jminor.unittest.username", "scott"),
          System.getProperty("jminor.unittest.password", "tiger"));
 
  @Test
  public void country() throws DatabaseException {
    testEntity(World.T_COUNTRY);
  }
 
  @Test
  public void city() throws DatabaseException {
    testEntity(World.T_CITY);
  }
 
  @Test
  public void countryLanguage() throws DatabaseException {
    testEntity(World.T_COUNTRYLANGUAGE);
  }
 
  @Override
  protected Entity initializeTestEntity(final String entityID) {
    final Entity entity = super.initializeTestEntity(entityID);
    if (entityID.equals(World.T_COUNTRY)) {
      entity.put(World.COUNTRY_CODE, "XXX");
      entity.put(World.COUNTRY_CONTINENT, "Asia");
    }
 
    return entity;
  }
 
  @Override
  protected void modifyEntity(final Entity testEntity) {
    super.modifyEntity(testEntity);
    if (testEntity.is(World.T_COUNTRY)) {
      testEntity.put(World.COUNTRY_CONTINENT, "Europe");
    }
  }
 
  @Override
  protected Entity initializeReferenceEntity(final String entityID) {
    switch (entityID) {
      case World.T_COUNTRY:
        final Entity iceland = Entities.entity(World.T_COUNTRY);
        iceland.put(World.COUNTRY_CODE, "ISL");
 
        return iceland;
      case World.T_CITY:
        final Entity reykjavik = Entities.entity(World.T_CITY);
        reykjavik.put(World.CITY_ID, 1449);
 
        return reykjavik;
    }
 
    return super.initializeReferenceEntity(entityID);
  }
 
  @Override
  protected User getTestUser() {
    return UNIT_TEST_USER;
  }
 
  @Override
  protected void loadDomainModel() {
    new World();
  }
}

Application UI layer

File: org/jminor/framework/demos/world/beans/ui/CountryEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.beans.ui;
 
import org.jminor.framework.demos.world.domain.World;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JTextField;
import java.awt.GridLayout;
 
public final class CountryEditPanel extends EntityEditPanel {
 
  private static final int COMBOBOX_WIDTH = 120;
  private static final int TEXT_FIELD_COLUMNS = 12;
 
  public CountryEditPanel(final SwingEntityEditModel editModel) {
    super(editModel);
  }
 
  @Override
  protected void initializeUI() {
    setInitialFocusProperty(World.COUNTRY_CODE);
 
    final JTextField txtCode = createTextField(World.COUNTRY_CODE);
    txtCode.setColumns(TEXT_FIELD_COLUMNS);
    UiUtil.makeUpperCase(txtCode);
    final JTextField txtCode2 = createTextField(World.COUNTRY_CODE2);
    txtCode2.setColumns(TEXT_FIELD_COLUMNS);
    UiUtil.makeUpperCase(txtCode2);
    createTextField(World.COUNTRY_NAME).setColumns(TEXT_FIELD_COLUMNS);
    UiUtil.setPreferredWidth(createPropertyComboBox(World.COUNTRY_CONTINENT), COMBOBOX_WIDTH);
    UiUtil.setPreferredWidth(createPropertyComboBox(World.COUNTRY_REGION), COMBOBOX_WIDTH);
    createTextField(World.COUNTRY_SURFACEAREA);
    createTextField(World.COUNTRY_INDEPYEAR);
    createTextField(World.COUNTRY_POPULATION);
    createTextField(World.COUNTRY_LIFEEXPECTANCY);
    createTextField(World.COUNTRY_GNP);
    createTextField(World.COUNTRY_GNPOLD);
    createTextField(World.COUNTRY_LOCALNAME).setColumns(TEXT_FIELD_COLUMNS);
    UiUtil.setPreferredWidth(createPropertyComboBox(World.COUNTRY_GOVERNMENTFORM), COMBOBOX_WIDTH);
    createTextField(World.COUNTRY_HEADOFSTATE).setColumns(TEXT_FIELD_COLUMNS);
    UiUtil.setPreferredWidth(createForeignKeyComboBox(World.COUNTRY_CAPITAL_FK), COMBOBOX_WIDTH);
 
    setLayout(new GridLayout(4, 5, 5, 5));
 
    addPropertyPanel(World.COUNTRY_CODE);
    addPropertyPanel(World.COUNTRY_CODE2);
    addPropertyPanel(World.COUNTRY_NAME);
    addPropertyPanel(World.COUNTRY_CONTINENT);
    addPropertyPanel(World.COUNTRY_REGION);
    addPropertyPanel(World.COUNTRY_SURFACEAREA);
    addPropertyPanel(World.COUNTRY_INDEPYEAR);
    addPropertyPanel(World.COUNTRY_POPULATION);
    addPropertyPanel(World.COUNTRY_LIFEEXPECTANCY);
    addPropertyPanel(World.COUNTRY_GNP);
    addPropertyPanel(World.COUNTRY_GNPOLD);
    addPropertyPanel(World.COUNTRY_LOCALNAME);
    addPropertyPanel(World.COUNTRY_GOVERNMENTFORM);
    addPropertyPanel(World.COUNTRY_HEADOFSTATE);
    addPropertyPanel(World.COUNTRY_CAPITAL_FK);
  }
}

File: org/jminor/framework/demos/world/beans/ui/CityEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.beans.ui;
 
import org.jminor.framework.demos.world.domain.World;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import java.awt.GridLayout;
 
public final class CityEditPanel extends EntityEditPanel {
 
  private static final int COMBOBOX_WIDTH = 120;
  private static final int TEXT_FIELD_COLUMNS = 12;
 
  public CityEditPanel(final SwingEntityEditModel editModel) {
    super(editModel);
  }
 
  @Override
  protected void initializeUI() {
    setInitialFocusProperty(World.CITY_COUNTRYCODE_FK);
 
    UiUtil.setPreferredWidth(createForeignKeyComboBox(World.CITY_COUNTRYCODE_FK), COMBOBOX_WIDTH);
    createTextField(World.CITY_NAME).setColumns(TEXT_FIELD_COLUMNS);
    createTextField(World.CITY_DISTRICT).setColumns(TEXT_FIELD_COLUMNS);
    createTextField(World.CITY_POPULATION);
 
    setLayout(new GridLayout(2, 2, 5, 5));
 
    addPropertyPanel(World.CITY_COUNTRYCODE_FK);
    addPropertyPanel(World.CITY_NAME);
    addPropertyPanel(World.CITY_DISTRICT);
    addPropertyPanel(World.CITY_POPULATION);
  }
}

File: org/jminor/framework/demos/world/beans/ui/CountryLanguageEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.beans.ui;
 
import org.jminor.framework.demos.world.domain.World;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import java.awt.GridLayout;
 
public final class CountryLanguageEditPanel extends EntityEditPanel {
 
  private static final int COMBOBOX_WIDTH = 120;
  private static final int TEXT_FIELD_COLUMNS = 12;
 
  public CountryLanguageEditPanel(final SwingEntityEditModel editModel) {
    super(editModel);
  }
 
  @Override
  protected void initializeUI() {
    setInitialFocusProperty(World.COUNTRYLANGUAGE_COUNTRYCODE_FK);
 
    UiUtil.setPreferredWidth(createForeignKeyComboBox(World.COUNTRYLANGUAGE_COUNTRYCODE_FK), COMBOBOX_WIDTH);
    createTextField(World.COUNTRYLANGUAGE_LANGUAGE).setColumns(TEXT_FIELD_COLUMNS);
    createCheckBox(World.COUNTRYLANGUAGE_ISOFFICIAL, null, false);
    createTextField(World.COUNTRYLANGUAGE_PERCENTAGE);
 
    setLayout(new GridLayout(2, 4, 5, 5));
 
    addPropertyPanel(World.COUNTRYLANGUAGE_COUNTRYCODE_FK);
    addPropertyPanel(World.COUNTRYLANGUAGE_LANGUAGE);
    addPropertyPanel(World.COUNTRYLANGUAGE_ISOFFICIAL);
    addPropertyPanel(World.COUNTRYLANGUAGE_PERCENTAGE);
  }
}

Main application panel

File: org/jminor/framework/demos/world/client/ui/WorldAppPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.world.client.ui;
 
import org.jminor.common.User;
import org.jminor.common.model.CancelException;
import org.jminor.framework.db.EntityConnectionProvider;
import org.jminor.framework.demos.world.beans.ui.CityEditPanel;
import org.jminor.framework.demos.world.beans.ui.CountryEditPanel;
import org.jminor.framework.demos.world.beans.ui.CountryLanguageEditPanel;
import org.jminor.framework.demos.world.domain.World;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.framework.model.SwingEntityApplicationModel;
import org.jminor.swing.framework.ui.EntityApplicationPanel;
import org.jminor.swing.framework.ui.EntityPanel;
import org.jminor.swing.framework.ui.EntityPanelProvider;
 
import java.util.Locale;
 
public final class WorldAppPanel extends EntityApplicationPanel<WorldAppPanel.WorldAppModel> {
 
  @Override
  protected void setupEntityPanelProviders() {
    final EntityPanelProvider countryPanelProvider = new EntityPanelProvider(World.T_COUNTRY);
    countryPanelProvider.setEditPanelClass(CountryEditPanel.class);
    final EntityPanelProvider cityPanelProvider = new EntityPanelProvider(World.T_CITY);
    cityPanelProvider.setEditPanelClass(CityEditPanel.class);
    final EntityPanelProvider countryLanguagePanelProvider = new EntityPanelProvider(World.T_COUNTRYLANGUAGE);
    countryLanguagePanelProvider.setEditPanelClass(CountryLanguageEditPanel.class);
    countryPanelProvider.addDetailPanelProvider(cityPanelProvider);
    countryPanelProvider.addDetailPanelProvider(countryLanguagePanelProvider);
 
    addEntityPanelProvider(countryPanelProvider);
  }
 
  @Override
  protected WorldAppModel initializeApplicationModel(final EntityConnectionProvider connectionProvider) {
    return new WorldAppModel(connectionProvider);
  }
 
  public static void main(final String[] args) throws CancelException {
    Locale.setDefault(new Locale("en", "EN"));
    EntityPanel.TOOLBAR_BUTTONS.set(true);
    EntityPanel.COMPACT_ENTITY_PANEL_LAYOUT.set(true);
    new WorldAppPanel().startApplication("World", null, false, UiUtil.getScreenSizeRatio(0.8), new User("scott", "tiger"));
  }
 
  public static final class WorldAppModel extends SwingEntityApplicationModel {
 
    private WorldAppModel(final EntityConnectionProvider connectionProvider) {
      super(connectionProvider);
    }
 
    @Override
    protected void loadDomainModel() {
      new World();
    }
  }
}
documentation/tutorials/world.txt · Last modified: 2016/05/27 13:06 by darri