DialogComponentDate set init/default value

I tried use the following code to set the DialogComponentDate init value,but not work,always 1970-01-01

m_startDate.setLocale(local);
m_startDate.getJCalendar().getCalendar().setTimeZone(timezone);
m_startDate.setDate(new Date());

and

    GWSettingsModelDate startDateSettings = new GWSettingsModelDate("startDate");
    startDateSettings.setTimeInMillis(System.currentTimeMillis());

I rewrite org.knime.examples.numberformatter.GWDateInputDialog.initialize(Locale, TimeZone) method to use another timezone and local

image

Hi @lou,

Thanks for reaching out!

Changing the value of your SettingsModelDate in the NodeDialog won’t work. The reason for this is that your NodeDialog obtains its value from the settings saved by the NodeModel (see https://www.knime.com/developer/example/node-model/load-save-settings for details).

What you have to do instead is set the default value of your SettingsModelDate in the NodeModel. Putting the following lines of code into your NodeModel should do the trick:

private final SettingsModelDate m_startDateSettings = createStartDateSettings();

static SettingsModelDate createStartDateSettings() {
    final SettingsModelDate startDateSettings = new SettingsModelDate("startDateSettings");
    startDateSettings.setTimeInMillis(System.currentTimeMillis());
    return startDateSettings;
}

@Override
protected void saveSettingsTo(final NodeSettingsWO settings) {
    m_startDateSettings.saveSettingsTo(settings);
}

@Override
protected void validateSettings(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_startDateSettings.validateSettings(settings);
}

@Override
protected void loadValidatedSettingsFrom(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_startDateSettings.loadSettingsFrom(settings);
}

You can then initialize your DialogComponentDate in your NodeDialog like so:

addDialogComponent(new DialogComponentDate(YourVeryOwnNodeModel.createStartDateSettings(), "some label"));

I hope this helped :slight_smile:

Marc

2 Likes

thanks,tried your solution
image

there are two date component starttime & endtime,the same code starttime work,but endtime not work

public static final GWSettingsModelDate startTimeSetting = createDefaultDateSetting(STARTTIME);
public static final GWSettingsModelDate endTimeSetting = createDefaultDateSetting(ENDTIME);


static GWSettingsModelDate createDefaultDateSetting(String key) {
	final GWSettingsModelDate defaultDateSetting = new GWSettingsModelDate(key);
	defaultDateSetting.setTimeInMillis(System.currentTimeMillis());
	return defaultDateSetting;
}

Why are your start and end time setting variables declared static?

en. What is the negative effect for using static setting variable?

I’m not sure this is the best forum for discussing OO things like this - but i am presuming you have N of these nodes, while KNIME is running, so it struck me as surprising that you’d have something named like you’re naming them as static in the model.
Maybe you’re using them in a different manner than i’m imagining them in your code - and so it makes sense that all the nodes would share this one value; so what i’m asking, rephrased, is “what is the goal by having all the nodes share the same start and end date models?”

got it.

in my sceneria,the node with starttime & endtime will be only one in general,you are right,i will change the variable declaration.

could you explain why the endtime default value not work?

Hi Lou that is a custom class by you, right? I think that might be the problem, is there a reason you are not using the SettingsModelDate?

best,
Gabriel

yes ,i want to change the timezone,so copy the code from SettingsModelDate
the starttime default value is correct.

private static final String VALUEKEY = "VALUEKEYSETTINGS";

private static final String STATUSKEY = "STATUSKEYSETTINGS";

private long m_millies = 0;

private int m_status = 0;

private final String m_configName;

/**
 * Returns the saved Date in milliseconds since 1970 in UTC time.
 *
 * @return time
 */
public long getTimeInMillis() {
    return m_millies;
}

/**
 * Returns the stored date.
 *
 * @return the user specified date configured in UTC
 */
public Date getDate() {
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Asia/Shanghai"));
    cal.setTimeInMillis(m_millies);

    return cal.getTime();

}

/**
 * sets the status code for the selected fields of the DateInputDialog.
 *
 * @param fields status code for the fields of the corresponding DateInputDialog
 *
 */
public void setSelectedFields(final int fields) {
    m_status = fields;
}

/**
 * sets the Date specified in the dialog in the long format.
 *
 * @param time time in milliseconds corresponding to the selected Date
 */
public void setTimeInMillis(final long time) {
    m_millies = time;
}

/**
 * sets the status code for the selected fields of the DateInputDialog.
 *
 * @return fields status code for the fields of the corresponding DateInputDialog
 */
public int getSelectedFields() {
    return m_status;
}

/**
 * Creates a new object holding a date value.
 *
 * @param configName the identifier the value is stored with in the {@link org.knime.core.node.NodeSettings} object
 */
public GWSettingsModelDate(final String configName) {
    if ((configName == null) || "".equals(configName)) {
        throw new IllegalArgumentException("The configName must be a " + "non-empty string");
    }
    m_configName = configName;
}

private GWSettingsModelDate(final String configname, final long value, final int status) {
    m_millies = value;
    m_status = status;
    m_configName = configname;

}

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
protected GWSettingsModelDate createClone() {
    return new GWSettingsModelDate(m_configName, m_millies, m_status);
}

/**
 * {@inheritDoc}
 */
@Override
protected String getModelTypeID() {
    return "SMID_DATE";
}

/**
 * {@inheritDoc}
 */
@Override
protected String getConfigName() {
    return m_configName;
}

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsForDialog(final NodeSettingsRO settings, final PortObjectSpec[] specs)
        throws NotConfigurableException {
    try {
        m_millies = settings.getLong(m_configName + VALUEKEY);
        m_status = settings.getInt(m_configName + STATUSKEY);
    } catch (InvalidSettingsException e) {
        m_millies = 0;
        m_status = 0;
    }

}

/**
 * {@inheritDoc}
 */
@Override
protected void saveSettingsForDialog(final NodeSettingsWO settings) throws InvalidSettingsException {
    settings.addLong(m_configName + VALUEKEY, m_millies);
    settings.addInt(m_configName + STATUSKEY, m_status);

}

/**
 * {@inheritDoc}
 */
@Override
protected void validateSettingsForModel(final NodeSettingsRO settings) throws InvalidSettingsException {

}

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsForModel(final NodeSettingsRO settings) throws InvalidSettingsException {
    m_millies = settings.getLong(m_configName + VALUEKEY);
    m_status = settings.getInt(m_configName + STATUSKEY);

}

/**
 * {@inheritDoc}
 */
@Override
protected void saveSettingsForModel(final NodeSettingsWO settings) {
    settings.addLong(m_configName + VALUEKEY, m_millies);
    settings.addInt(m_configName + STATUSKEY, m_status);

}

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    return getClass().getSimpleName() + " ('" + m_configName + "')";
}

You’ve provided not a lot of code to look at, so i couldn’t begin to guess what your mistake is.

I’m inferring from the way you chose not to subclass SettingsModelDate (or subclassed it excessively - which one, i can’t tell as you didn’t include the class declaration) that writing OO code is something new to you and, as such, i imagine there are mistakes abound in what you’ve written.

Resolved ,
forgot call savesettings method with enddatesetting

1 Like