JMinor Application Framework

As simple as possible but not simpler

User Tools

Site Tools


documentation:tutorials:petstore

Pet Store: Application tutorial

Data

Define the domain model

File: org/jminor/framework/demos/petstore/domain/Petstore.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.domain;
 
import org.jminor.framework.domain.Entities;
import org.jminor.framework.domain.Properties;
 
import java.sql.Types;
 
public class Petstore {
 
  private Petstore() {}
  public static void init() {}
 
  public static final String VERSION = "@petstore-v1";
 
  public static final String T_ADDRESS = "address" + VERSION;
  public static final String ADDRESS_ID = "addressid";
  public static final String ADDRESS_STREET_1 = "street1";
  public static final String ADDRESS_STREET_2 = "street2";
  public static final String ADDRESS_CITY = "city";
  public static final String ADDRESS_STATE = "state";
  public static final String ADDRESS_ZIP = "zip";
  public static final String ADDRESS_ZIP_FK = "zip_fk";
  public static final String ADDRESS_LATITUDE = "latitude";
  public static final String ADDRESS_LONGITUDE = "longitude";
 
  static {
    Entities.define(T_ADDRESS, "petstore.address",
            Properties.primaryKeyProperty(ADDRESS_ID),
            Properties.columnProperty(ADDRESS_STREET_1, Types.VARCHAR, "Street 1").setMaxLength(55).setNullable(false),
            Properties.columnProperty(ADDRESS_STREET_2, Types.VARCHAR, "Street 2").setMaxLength(55),
            Properties.columnProperty(ADDRESS_CITY, Types.VARCHAR, "City").setMaxLength(55).setNullable(false),
            Properties.columnProperty(ADDRESS_STATE, Types.VARCHAR, "State").setMaxLength(25).setNullable(false),
            Properties.columnProperty(ADDRESS_ZIP, Types.INTEGER, "Zip").setNullable(false),
            Properties.columnProperty(ADDRESS_LATITUDE, Types.DOUBLE, "Latitude").setNullable(false).setMaximumFractionDigits(2),
            Properties.columnProperty(ADDRESS_LONGITUDE, Types.DOUBLE, "Longitude").setNullable(false).setMaximumFractionDigits(2))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.address", ADDRESS_ID))
            .setOrderByClause(ADDRESS_CITY + ", " + ADDRESS_STREET_1 + ", " + ADDRESS_STREET_2)
            .setStringProvider(new Entities.StringProvider(ADDRESS_STREET_1).addText(" ")
            .addValue(ADDRESS_STREET_2).addText(", ").addValue(ADDRESS_CITY).addText(" ")
            .addValue(ADDRESS_ZIP).addText(", ").addValue(ADDRESS_STATE))
            .setCaption("Addresses");
  }
 
  public static final String T_CATEGORY = "category" + VERSION;
  public static final String CATEGORY_ID = "categoryid";
  public static final String CATEGORY_NAME = "name";
  public static final String CATEGORY_DESCRIPTION = "description";
  public static final String CATEGORY_IMAGE_URL = "imageurl";
 
  static {
    Entities.define(T_CATEGORY, "petstore.category",
            Properties.primaryKeyProperty(CATEGORY_ID),
            Properties.columnProperty(CATEGORY_NAME, Types.VARCHAR, "Name").setMaxLength(25).setNullable(false),
            Properties.columnProperty(CATEGORY_DESCRIPTION, Types.VARCHAR, "Description").setMaxLength(255).setNullable(false),
            Properties.columnProperty(CATEGORY_IMAGE_URL, Types.VARCHAR, "Image URL").setHidden(true))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.category", CATEGORY_ID))
            .setOrderByClause(CATEGORY_NAME)
            .setStringProvider(new Entities.StringProvider(CATEGORY_NAME))
            .setCaption("Categories");
  }
 
  public static final String T_PRODUCT = "product" + VERSION;
  public static final String PRODUCT_ID = "productid";
  public static final String PRODUCT_CATEGORY_ID = "categoryid";
  public static final String PRODUCT_CATEGORY_FK = "category_fk";
  public static final String PRODUCT_NAME = "name";
  public static final String PRODUCT_DESCRIPTION = "description";
  public static final String PRODUCT_IMAGE_URL = "imageurl";
 
  static {
    Entities.define(T_PRODUCT, "petstore.product",
            Properties.primaryKeyProperty(PRODUCT_ID),
            Properties.foreignKeyProperty(PRODUCT_CATEGORY_FK, "Category", T_CATEGORY,
                    Properties.columnProperty(PRODUCT_CATEGORY_ID)).setNullable(false),
            Properties.columnProperty(PRODUCT_NAME, Types.VARCHAR, "Name").setMaxLength(25).setNullable(false),
            Properties.columnProperty(PRODUCT_DESCRIPTION, Types.VARCHAR, "Description").setMaxLength(255).setNullable(false),
            Properties.columnProperty(PRODUCT_IMAGE_URL, Types.VARCHAR, "Image URL").setMaxLength(55).setHidden(true))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.product", PRODUCT_ID))
            .setOrderByClause(PRODUCT_NAME)
            .setStringProvider(new Entities.StringProvider(PRODUCT_CATEGORY_FK)
            .addText(" - ").addValue(PRODUCT_NAME))
            .setCaption("Products");
  }
 
  public static final String T_SELLER_CONTACT_INFO = "sellercontactinfo" + VERSION;
  public static final String SELLER_CONTACT_INFO_ID = "contactinfoid";
  public static final String SELLER_CONTACT_INFO_FIRST_NAME = "firstname";
  public static final String SELLER_CONTACT_INFO_LAST_NAME = "lastname";
  public static final String SELLER_CONTACT_INFO_EMAIL = "email";
 
  static {
    Entities.define(T_SELLER_CONTACT_INFO, "petstore.sellercontactinfo",
            Properties.primaryKeyProperty(SELLER_CONTACT_INFO_ID),
            Properties.columnProperty(SELLER_CONTACT_INFO_FIRST_NAME, Types.VARCHAR, "First name").setMaxLength(24).setNullable(false),
            Properties.columnProperty(SELLER_CONTACT_INFO_LAST_NAME, Types.VARCHAR, "Last name").setMaxLength(24).setNullable(false),
            Properties.columnProperty(SELLER_CONTACT_INFO_EMAIL, Types.VARCHAR, "Email").setMaxLength(24).setNullable(false))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.sellercontactinfo", SELLER_CONTACT_INFO_ID))
            .setOrderByClause(SELLER_CONTACT_INFO_LAST_NAME + ", "+ SELLER_CONTACT_INFO_FIRST_NAME)
            .setStringProvider(new Entities.StringProvider(SELLER_CONTACT_INFO_LAST_NAME)
            .addText(", ").addValue(SELLER_CONTACT_INFO_FIRST_NAME))
            .setCaption("Seller info");
  }
 
  public static final String T_ITEM = "item" + VERSION;
  public static final String ITEM_ID = "itemid";
  public static final String ITEM_PRODUCT_ID = "productid";
  public static final String ITEM_PRODUCT_FK = "product_fk";
  public static final String ITEM_NAME = "name";
  public static final String ITEM_DESCRIPTION = "description";
  public static final String ITEM_IMAGE_URL = "imageurl";
  public static final String ITEM_IMAGE_THUMB_URL = "imagethumburl";
  public static final String ITEM_PRICE = "price";
  public static final String ITEM_ADDRESS_ID = "address_addressid";
  public static final String ITEM_ADDRESS_FK = "address_fk";
  public static final String ITEM_C0NTACT_INFO_ID = "contactinfo_contactinfoid";
  public static final String ITEM_C0NTACT_INFO_FK = "contactinfo_fk";
  public static final String ITEM_DISABLED = "disabled";
 
  static {
    Entities.define(T_ITEM, "petstore.item",
            Properties.primaryKeyProperty(ITEM_ID),
            Properties.foreignKeyProperty(ITEM_PRODUCT_FK, "Product", T_PRODUCT,
                    Properties.columnProperty(ITEM_PRODUCT_ID)).setFetchDepth(2).setNullable(false),
            Properties.columnProperty(ITEM_NAME, Types.VARCHAR, "Name").setMaxLength(30).setNullable(false),
            Properties.columnProperty(ITEM_DESCRIPTION, Types.VARCHAR, "Description").setMaxLength(500).setNullable(false),
            Properties.columnProperty(ITEM_IMAGE_URL, Types.VARCHAR, "Image URL").setMaxLength(55).setHidden(true),
            Properties.columnProperty(ITEM_IMAGE_THUMB_URL, Types.VARCHAR, "Image thumbnail URL").setMaxLength(55).setHidden(true),
            Properties.columnProperty(ITEM_PRICE, Types.DOUBLE, "Price").setNullable(false).setMaximumFractionDigits(2),
            Properties.foreignKeyProperty(ITEM_C0NTACT_INFO_FK, "Contact info", T_SELLER_CONTACT_INFO,
                    Properties.columnProperty(ITEM_C0NTACT_INFO_ID)).setNullable(false),
            Properties.foreignKeyProperty(ITEM_ADDRESS_FK, "Address", T_ADDRESS,
                    Properties.columnProperty(ITEM_ADDRESS_ID)).setNullable(false),
            Properties.booleanProperty(ITEM_DISABLED, Types.INTEGER, "Disabled", 1, 0).setDefaultValue(false))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.item", ITEM_ID))
            .setOrderByClause(ITEM_NAME)
            .setStringProvider(new Entities.StringProvider(ITEM_PRODUCT_FK).addText(" - ").addValue(ITEM_NAME))
            .setCaption("Items");
  }
 
  public static final String T_TAG = "tag" + VERSION;
  public static final String TAG_ID = "tagid";
  public static final String TAG_TAG = "tag";
  public static final String TAG_REFCOUNT = "refcount";
 
  static {
    Entities.define(T_TAG, "petstore.tag",
            Properties.primaryKeyProperty(TAG_ID),
            Properties.columnProperty(TAG_TAG, Types.VARCHAR, "Tag").setMaxLength(30).setNullable(false),
            Properties.subqueryProperty(TAG_REFCOUNT, Types.INTEGER, "Reference count",
                    "select count(*) from petstore.tag_item  where tagid = tag.tagid"))
            .setKeyGenerator(Entities.incrementKeyGenerator("petstore.tag", TAG_ID))
            .setOrderByClause(TAG_TAG)
            .setSelectTableName("petstore.tag tag")
            .setStringProvider(new Entities.StringProvider(TAG_TAG))
            .setCaption("Tags");
  }
 
  public static final String T_TAG_ITEM = "tag_item" + VERSION;
  public static final String TAG_ITEM_TAG_ID = "tagid";
  public static final String TAG_ITEM_TAG_FK = "tag_fk";
  public static final String TAG_ITEM_ITEM_ID = "itemid";
  public static final String TAG_ITEM_ITEM_FK = "item_fk";
 
  static {
    Entities.define(T_TAG_ITEM, "petstore.tag_item",
            Properties.foreignKeyProperty(TAG_ITEM_ITEM_FK, "Item", T_ITEM,
                    Properties.primaryKeyProperty(TAG_ITEM_ITEM_ID, Types.INTEGER).setPrimaryKeyIndex(0)).setNullable(false),
            Properties.foreignKeyProperty(TAG_ITEM_TAG_FK, "Tag", T_TAG,
                    Properties.primaryKeyProperty(TAG_ITEM_TAG_ID, Types.INTEGER).setPrimaryKeyIndex(1)).setNullable(false))
            .setStringProvider(new Entities.StringProvider(TAG_ITEM_ITEM_FK).addText(" - ").addValue(TAG_ITEM_TAG_FK))
            .setCaption("Item tags");
  }
}

Unit testing the domain model

File: org/jminor/framework/demos/petstore/domain/PetstoreTest.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.domain;
 
import org.jminor.common.User;
import org.jminor.common.model.CancelException;
import org.jminor.framework.domain.testing.EntityTestUnit;
 
import org.junit.Test;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class PetstoreTest 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 address() throws Exception {
    testEntity(T_ADDRESS);
  }
 
  @Test
  public void category() throws Exception {
    testEntity(T_CATEGORY);
  }
 
  @Test
  public void item() throws Exception {
    testEntity(T_ITEM);
  }
 
  @Test
  public void product() throws Exception {
    testEntity(T_PRODUCT);
  }
 
  @Test
  public void sellerInfo() throws Exception {
    testEntity(T_SELLER_CONTACT_INFO);
  }
 
  @Test
  public void tag() throws Exception {
    testEntity(T_TAG);
  }
 
  @Test
  public void tagItem() throws Exception {
    testEntity(T_TAG_ITEM);
  }
 
  @Override
  protected void loadDomainModel() {
    Petstore.init();
  }
 
  @Override
  protected User getTestUser() throws CancelException {
    return UNIT_TEST_USER;
  }
}

CRUD UI layer

File: org/jminor/framework/demos/petstore/beans/ui/AddressEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JLabel;
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class AddressEditPanel extends EntityEditPanel {
 
  public AddressEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(4,2,5,5));
    JTextField txt = createTextField(ADDRESS_CITY);
    setInitialFocusComponent(txt);
    txt.setColumns(12);
    addPropertyPanel(ADDRESS_CITY);
    txt = createTextField(ADDRESS_STATE);
    txt.setColumns(12);
    addPropertyPanel(ADDRESS_STATE);
    add(new JLabel());
    txt = createTextField(ADDRESS_ZIP);
    txt.setColumns(12);
    addPropertyPanel(ADDRESS_ZIP);
    txt = createTextField(ADDRESS_STREET_1);
    txt.setColumns(12);
    addPropertyPanel(ADDRESS_STREET_1);
    txt = createTextField(ADDRESS_STREET_2);
    txt.setColumns(12);
    addPropertyPanel(ADDRESS_STREET_2);
    createTextField(ADDRESS_LATITUDE);
    addPropertyPanel(ADDRESS_LATITUDE);
    createTextField(ADDRESS_LONGITUDE);
    addPropertyPanel(ADDRESS_LONGITUDE);
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/CategoryEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class CategoryEditPanel extends EntityEditPanel {
 
  public CategoryEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(2,2,5,5));
    final JTextField txtName = createTextField(CATEGORY_NAME);
    setInitialFocusComponent(txtName);
    txtName.setColumns(10);
    addPropertyPanel(CATEGORY_NAME);
    final JTextField txtDesc = createTextField(CATEGORY_DESCRIPTION);
    txtDesc.setColumns(18);
    addPropertyPanel(CATEGORY_DESCRIPTION);
    createTextField(CATEGORY_IMAGE_URL);
    addPropertyPanel(CATEGORY_IMAGE_URL);
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/ContactInfoEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class ContactInfoEditPanel extends EntityEditPanel {
 
  public ContactInfoEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(3,1,5,5));
    final JTextField txt = createTextField(SELLER_CONTACT_INFO_LAST_NAME);
    setInitialFocusComponent(txt);
    txt.setColumns(10);
    addPropertyPanel(SELLER_CONTACT_INFO_LAST_NAME);
    createTextField(SELLER_CONTACT_INFO_FIRST_NAME);
    addPropertyPanel(SELLER_CONTACT_INFO_FIRST_NAME);
    createTextField(SELLER_CONTACT_INFO_EMAIL);
    addPropertyPanel(SELLER_CONTACT_INFO_EMAIL);
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/ItemEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.framework.demos.petstore.domain.Petstore;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityComboBox;
import org.jminor.swing.framework.ui.EntityEditPanel;
import org.jminor.swing.framework.ui.EntityPanelProvider;
import org.jminor.swing.framework.ui.EntityUiUtil;
 
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class ItemEditPanel extends EntityEditPanel {
 
  public ItemEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(3,3,5,5));
    EntityComboBox box = createForeignKeyComboBox(ITEM_PRODUCT_FK);
    setInitialFocusComponent(box);
    addPropertyPanel(ITEM_PRODUCT_FK);
    JTextField txt = createTextField(ITEM_NAME);
    txt.setColumns(12);
    addPropertyPanel(ITEM_NAME);
    txt = createTextField(ITEM_DESCRIPTION);
    txt.setColumns(16);
    addPropertyPanel(ITEM_DESCRIPTION);
    createTextField(ITEM_PRICE);
    addPropertyPanel(ITEM_PRICE);
    box = createForeignKeyComboBox(ITEM_C0NTACT_INFO_FK);
    box.setPopupWidth(200);
    box.setPreferredSize(UiUtil.getPreferredTextFieldSize());
    add(createPropertyPanel(ITEM_C0NTACT_INFO_FK, EntityUiUtil.createEastButtonPanel(box,
            createEditPanelAction(box, new EntityPanelProvider(Petstore.T_SELLER_CONTACT_INFO).setEditPanelClass(ContactInfoEditPanel.class)), false)));
    box = createForeignKeyComboBox(ITEM_ADDRESS_FK);
    box.setPopupWidth(200);
    box.setPreferredSize(UiUtil.getPreferredTextFieldSize());
    add(createPropertyPanel(ITEM_ADDRESS_FK, EntityUiUtil.createEastButtonPanel(box,
            createEditPanelAction(box, new EntityPanelProvider(Petstore.T_ADDRESS).setEditPanelClass(AddressEditPanel.class)), false)));
    createTextField(ITEM_IMAGE_URL);
    addPropertyPanel(ITEM_IMAGE_URL);
    createTextField(ITEM_IMAGE_THUMB_URL);
    addPropertyPanel(ITEM_IMAGE_THUMB_URL);
    createTristateCheckBox(ITEM_DISABLED, null, false);
    addPropertyPanel(ITEM_DISABLED);
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/ProductEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityComboBox;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.*;
 
public class ProductEditPanel extends EntityEditPanel {
 
  public ProductEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(3,1,5,5));
    final EntityComboBox box = createForeignKeyComboBox(PRODUCT_CATEGORY_FK);
    setInitialFocusComponent(box);
    addPropertyPanel(PRODUCT_CATEGORY_FK);
    createTextField(PRODUCT_NAME);
    addPropertyPanel(PRODUCT_NAME);
    final JTextField txt = createTextField(PRODUCT_DESCRIPTION);
    txt.setColumns(16);
    addPropertyPanel(PRODUCT_DESCRIPTION);
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/TagItemEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.framework.demos.petstore.domain.Petstore;
import org.jminor.swing.common.ui.UiUtil;
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityComboBox;
import org.jminor.swing.framework.ui.EntityEditPanel;
import org.jminor.swing.framework.ui.EntityPanelProvider;
import org.jminor.swing.framework.ui.EntityUiUtil;
 
import java.awt.Dimension;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.TAG_ITEM_ITEM_FK;
import static org.jminor.framework.demos.petstore.domain.Petstore.TAG_ITEM_TAG_FK;
 
public class TagItemEditPanel extends EntityEditPanel {
 
  public TagItemEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(2,1,5,5));
    EntityComboBox box = createForeignKeyComboBox(TAG_ITEM_ITEM_FK);
    setInitialFocusComponent(box);
    box.setPopupWidth(240);
    box.setPreferredSize(new Dimension(180, UiUtil.getPreferredTextFieldHeight()));
    addPropertyPanel(TAG_ITEM_ITEM_FK);
    box = createForeignKeyComboBox(TAG_ITEM_TAG_FK);
    add(createPropertyPanel(TAG_ITEM_TAG_FK, EntityUiUtil.createEastButtonPanel(box,
            createEditPanelAction(box, new EntityPanelProvider(Petstore.T_TAG).setEditPanelClass(TagEditPanel.class)), false)));
  }
}

File: org/jminor/framework/demos/petstore/beans/ui/TagEditPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.beans.ui;
 
import org.jminor.swing.common.ui.layout.FlexibleGridLayout;
import org.jminor.swing.framework.model.SwingEntityEditModel;
import org.jminor.swing.framework.ui.EntityEditPanel;
 
import javax.swing.JTextField;
 
import static org.jminor.framework.demos.petstore.domain.Petstore.TAG_TAG;
 
public class TagEditPanel extends EntityEditPanel {
 
  public TagEditPanel(final SwingEntityEditModel model) {
    super(model);
  }
 
  @Override
  protected void initializeUI() {
    setLayout(new FlexibleGridLayout(1,1,5,5));
    final JTextField txt = createTextField(TAG_TAG);
    setInitialFocusComponent(txt);
    txt.setColumns(16);
    addPropertyPanel(TAG_TAG);
  }
}

Main application panel

File: org/jminor/framework/demos/petstore/client/ui/PetstoreAppPanel.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.client.ui;
 
import org.jminor.common.User;
import org.jminor.common.model.CancelException;
import org.jminor.framework.db.EntityConnectionProvider;
import org.jminor.framework.demos.petstore.beans.ui.AddressEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.CategoryEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.ContactInfoEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.ItemEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.ProductEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.TagEditPanel;
import org.jminor.framework.demos.petstore.beans.ui.TagItemEditPanel;
import org.jminor.framework.demos.petstore.domain.Petstore;
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 PetstoreAppPanel extends EntityApplicationPanel<PetstoreAppPanel.PetstoreApplicationModel> {
 
  @Override
  protected void setupEntityPanelProviders() {
   /* CATEGORY
    *   PRODUCT
    *     ITEM
    *       ITEMTAG
    */
    final EntityPanelProvider tagItemProvider = new EntityPanelProvider(Petstore.T_TAG_ITEM).setEditPanelClass(TagItemEditPanel.class);
    final EntityPanelProvider itemProvider = new EntityPanelProvider(Petstore.T_ITEM).setEditPanelClass(ItemEditPanel.class);
    itemProvider.addDetailPanelProvider(tagItemProvider).setDetailPanelState(EntityPanel.PanelState.HIDDEN);
    final EntityPanelProvider productProvider = new EntityPanelProvider(Petstore.T_PRODUCT).setEditPanelClass(ProductEditPanel.class);
    productProvider.addDetailPanelProvider(itemProvider).setDetailSplitPanelResizeWeight(0.3);
    final EntityPanelProvider categoryProvider = new EntityPanelProvider(Petstore.T_CATEGORY).setEditPanelClass(CategoryEditPanel.class);
    categoryProvider.addDetailPanelProvider(productProvider).setDetailSplitPanelResizeWeight(0.3);
 
    addEntityPanelProvider(categoryProvider);
 
    final EntityPanelProvider addressProvider = new EntityPanelProvider(Petstore.T_ADDRESS).setEditPanelClass(AddressEditPanel.class);
    final EntityPanelProvider contactInfoProvider = new EntityPanelProvider(Petstore.T_SELLER_CONTACT_INFO).setEditPanelClass(ContactInfoEditPanel.class);
    contactInfoProvider.addDetailPanelProvider(itemProvider);
    final EntityPanelProvider tagProvider = new EntityPanelProvider(Petstore.T_TAG).setEditPanelClass(TagEditPanel.class);
    tagProvider.addDetailPanelProvider(tagItemProvider).setDetailPanelState(EntityPanel.PanelState.HIDDEN);
 
    addSupportPanelProviders(addressProvider, contactInfoProvider, tagProvider);
  }
 
  @Override
  protected PetstoreApplicationModel initializeApplicationModel(final EntityConnectionProvider connectionProvider) throws CancelException {
    return new PetstoreApplicationModel(connectionProvider);
  }
 
  public static void main(final String[] args) {
    Locale.setDefault(new Locale("en"));
    EntityPanel.TOOLBAR_BUTTONS.set(true);
    new PetstoreAppPanel().startApplication("The Pet Store", null, false, UiUtil.getScreenSizeRatio(0.8), new User("scott", "tiger"));
  }
 
  public static final class PetstoreApplicationModel extends SwingEntityApplicationModel {
    public PetstoreApplicationModel(final EntityConnectionProvider connectionProvider) {
      super(connectionProvider);
    }
 
    @Override
    protected void loadDomainModel() {
      Petstore.init();
    }
  }
}

Application load test

File: org/jminor/framework/demos/petstore/testing/PetstoreLoadTest.java -

/*
 * Copyright (c) 2004 - 2017, Björn Darri Sigurðsson. All Rights Reserved.
 */
package org.jminor.framework.demos.petstore.testing;
 
import org.jminor.common.User;
import org.jminor.common.model.CancelException;
import org.jminor.common.server.Server;
import org.jminor.framework.db.remote.RemoteEntityConnectionProvider;
import org.jminor.framework.demos.petstore.client.ui.PetstoreAppPanel;
import org.jminor.framework.model.EntityLoadTestModel;
import org.jminor.swing.common.ui.tools.LoadTestPanel;
import org.jminor.swing.framework.model.SwingEntityModel;
 
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.util.Collections;
import java.util.UUID;
 
public final class PetstoreLoadTest extends EntityLoadTestModel<PetstoreAppPanel.PetstoreApplicationModel> {
 
  private static final User UNIT_TEST_USER = new User(
          System.getProperty("jminor.unittest.username", "scott"),
          System.getProperty("jminor.unittest.password", "tiger"));
 
  public PetstoreLoadTest() {
    super(UNIT_TEST_USER, Collections.singletonList(new AbstractUsageScenario<PetstoreAppPanel.PetstoreApplicationModel>("selectRecords") {
      @Override
      protected void performScenario(final PetstoreAppPanel.PetstoreApplicationModel application) {
        final SwingEntityModel categoryModel = application.getEntityModels().iterator().next();
        categoryModel.getTableModel().getSelectionModel().clearSelection();
        categoryModel.refresh();
        selectRandomRow(categoryModel.getTableModel());
        selectRandomRow(categoryModel.getDetailModels().iterator().next().getTableModel());
        selectRandomRow(categoryModel.getDetailModels().iterator().next().getDetailModels().iterator().next().getTableModel());
      }
    }));
  }
 
  @Override
  protected PetstoreAppPanel.PetstoreApplicationModel initializeApplication() throws CancelException {
    final PetstoreAppPanel.PetstoreApplicationModel applicationModel = new PetstoreAppPanel.PetstoreApplicationModel(
            new RemoteEntityConnectionProvider(Server.SERVER_HOST_NAME.get(),
                    getUser(), UUID.randomUUID(), getClass().getSimpleName()));
    final SwingEntityModel categoryModel = applicationModel.getEntityModels().iterator().next();
    categoryModel.addLinkedDetailModel(categoryModel.getDetailModels().iterator().next());
    final SwingEntityModel productModel = categoryModel.getDetailModels().iterator().next();
    productModel.addLinkedDetailModel(productModel.getDetailModels().iterator().next());
    final SwingEntityModel itemModel = productModel.getDetailModels().iterator().next();
    itemModel.addLinkedDetailModel(itemModel.getDetailModels().iterator().next());
 
    return applicationModel;
  }
 
  public static void main(final String[] args) throws Exception {
    SwingUtilities.invokeLater(new Runner());
  }
 
  private static final class Runner implements Runnable {
    @Override
    public void run() {
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        new LoadTestPanel(new PetstoreLoadTest()).showFrame();
      }
      catch (final Exception e) {
        e.printStackTrace();
      }
    }
  }
}
documentation/tutorials/petstore.txt · Last modified: 2015/10/29 16:25 by darri