/**
*
* Copyright
* 2009-2015 Jayway Products AB
* 2016-2017 Föreningen Sambruk
*
* Licensed under AGPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/agpl.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.streamsource.streamflow.client.ui.administration.resolutions;
import static se.streamsource.streamflow.client.util.i18n.text;
import java.awt.BorderLayout;
import javax.swing.ActionMap;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import org.jdesktop.application.Action;
import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.Task;
import org.qi4j.api.injection.scope.Service;
import org.qi4j.api.injection.scope.Structure;
import org.qi4j.api.injection.scope.Uses;
import org.qi4j.api.structure.Module;
import org.qi4j.api.util.Iterables;
import se.streamsource.dci.value.link.LinkValue;
import se.streamsource.streamflow.client.ui.administration.AdministrationResources;
import se.streamsource.streamflow.client.util.CommandTask;
import se.streamsource.streamflow.client.util.LinkListCellRenderer;
import se.streamsource.streamflow.client.util.RefreshWhenShowing;
import se.streamsource.streamflow.client.util.SelectionActionEnabler;
import se.streamsource.streamflow.client.util.StreamflowButton;
import se.streamsource.streamflow.client.util.dialog.DialogService;
import se.streamsource.streamflow.client.util.dialog.SelectLinkDialog;
import se.streamsource.streamflow.infrastructure.event.domain.TransactionDomainEvents;
import se.streamsource.streamflow.infrastructure.event.domain.source.TransactionListener;
import ca.odell.glazedlists.swing.EventListModel;
import com.jgoodies.forms.factories.Borders;
/**
* JAVADOC
*/
public class SelectedResolutionsView
extends JPanel
implements TransactionListener
{
@Service
DialogService dialogs;
@Structure
Module module;
private JList list;
private SelectedResolutionsModel model;
public SelectedResolutionsView( @Service ApplicationContext context,
@Uses final SelectedResolutionsModel model)
{
super( new BorderLayout() );
this.model = model;
setBorder( Borders.createEmptyBorder( "2dlu, 2dlu, 2dlu, 2dlu" ) );
ActionMap am = context.getActionMap( this );
setActionMap( am );
list = new JList( new EventListModel<LinkValue>( model.getList() ) );
list.setCellRenderer( new LinkListCellRenderer() );
add( new JScrollPane( list ), BorderLayout.CENTER );
JPanel toolbar = new JPanel();
toolbar.add( new StreamflowButton( am.get( "add" ) ) );
toolbar.add( new StreamflowButton( am.get( "remove" ) ) );
add( toolbar, BorderLayout.SOUTH );
list.getSelectionModel().addListSelectionListener( new SelectionActionEnabler( am.get( "remove" ) ) );
new RefreshWhenShowing( this, model );
}
@Action
public Task add()
{
final SelectLinkDialog dialog = module.objectBuilderFactory().newObjectBuilder(SelectLinkDialog.class).use( model.getPossible() ).newInstance();
dialog.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
dialogs.showOkCancelHelpDialog( this, dialog, text( AdministrationResources.choose_resolution_title ) );
if (dialog.getSelectedLinks() != null)
{
return new CommandTask()
{
@Override
public void command()
throws Exception
{
model.add( dialog.getSelectedLinks() );
}
};
} else
return null;
}
@Action
public Task remove()
{
final Iterable<LinkValue> selected = (Iterable) Iterables.iterable( list.getSelectedValues() );
return new CommandTask()
{
@Override
public void command()
throws Exception
{
for (LinkValue linkValue : selected)
{
model.remove( linkValue );
}
}
};
}
public void notifyTransactions( Iterable<TransactionDomainEvents> transactions )
{
model.notifyTransactions( transactions );
}
}