Skip to content

Commit

Permalink
Link table implementation on the manager for both feature and tile ta…
Browse files Browse the repository at this point in the history
…bles.
  • Loading branch information
bosborn committed Feb 1, 2016
1 parent b0e68f6 commit cea0de2
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 7 deletions.
222 changes: 215 additions & 7 deletions mapcache/src/main/java/mil/nga/mapcache/GeoPackageManagerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
Expand All @@ -54,8 +55,10 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import mil.nga.geopackage.BoundingBox;
import mil.nga.geopackage.GeoPackage;
Expand All @@ -66,6 +69,9 @@
import mil.nga.geopackage.core.contents.ContentsDao;
import mil.nga.geopackage.core.srs.SpatialReferenceSystem;
import mil.nga.geopackage.core.srs.SpatialReferenceSystemDao;
import mil.nga.geopackage.extension.Extensions;
import mil.nga.geopackage.extension.link.FeatureTileLink;
import mil.nga.geopackage.extension.link.FeatureTileTableLinker;
import mil.nga.geopackage.factory.GeoPackageFactory;
import mil.nga.geopackage.features.columns.GeometryColumns;
import mil.nga.geopackage.features.columns.GeometryColumnsDao;
Expand Down Expand Up @@ -304,7 +310,7 @@ public void update(boolean includeExternal) {
databases = manager.internalDatabases();

// Disable any active external databases
for(String externalDatabase: manager.externalDatabaseSet()) {
for (String externalDatabase : manager.externalDatabaseSet()) {
active.removeDatabase(externalDatabase, true);
}
}
Expand Down Expand Up @@ -732,10 +738,10 @@ private void exportDatabaseOptionCheckPermissions(final String database) {
*
* @param granted true if permission was granted
*/
public void exportDatabaseAfterPermission(boolean granted){
if(granted) {
public void exportDatabaseAfterPermission(boolean granted) {
if (granted) {
exportDatabaseOption(exportDatabaseName);
}else{
} else {
showDisabledExternalExportPermissionsDialog();
}
}
Expand Down Expand Up @@ -1281,10 +1287,12 @@ private void tableOptions(final GeoPackageTable table) {
adapter.add(getString(R.string.geopackage_table_index_features_label));
adapter.add(getString(R.string.geopackage_table_create_feature_tiles_label));
adapter.add(getString(R.string.geopackage_table_add_feature_overlay_label));
adapter.add(getString(R.string.geopackage_table_link_label));
break;

case TILE:
adapter.add(getString(R.string.geopackage_table_tiles_load_label));
adapter.add(getString(R.string.geopackage_table_link_label));
break;

case FEATURE_OVERLAY:
Expand Down Expand Up @@ -1347,6 +1355,9 @@ public void onClick(DialogInterface dialog, int item) {
case FEATURE:
createFeatureTilesTableOption(table);
break;
case TILE:
linkTableOption(table);
break;
}
break;
case 5:
Expand All @@ -1356,7 +1367,13 @@ public void onClick(DialogInterface dialog, int item) {
break;
}
break;

case 6:
switch (table.getType()) {
case FEATURE:
linkTableOption(table);
break;
}
break;
default:
}
}
Expand Down Expand Up @@ -2661,6 +2678,137 @@ public void onClick(DialogInterface dialog, int id) {
dialog.show();
}

/**
* Link table option
*
* @param table
*/
private void linkTableOption(final GeoPackageTable table) {

// Get a feature tile table linker
GeoPackageManager manager = GeoPackageFactory.getManager(getActivity());
GeoPackage geoPackage = manager.open(table.getDatabase());
FeatureTileTableLinker linker = new FeatureTileTableLinker(geoPackage);

// Get the tables that can be linked and the currently linked tables
final List<String> tables = new ArrayList<>();
List<FeatureTileLink> linkedTables = null;
switch (table.getType()) {
case FEATURE:
tables.addAll(geoPackage.getTileTables());
linkedTables = linker.queryForFeatureTable(table.getName());
break;
case TILE:
tables.addAll(geoPackage.getFeatureTables());
linkedTables = linker.queryForTileTable(table.getName());
break;
default:
throw new GeoPackageException("Unexpected table type: " + table.getType());
}

// Close the GeoPackage
geoPackage.close();

// Build a set of currently linked tables
final Set<String> linkedTableSet = new HashSet<>();
for(FeatureTileLink link: linkedTables){
switch(table.getType()) {
case FEATURE:
linkedTableSet.add(link.getTileTableName());
break;
case TILE:
linkedTableSet.add(link.getFeatureTableName());
break;
}
}

// Maintain a copy of the linked tables before changes get made
final Set<String> originalLinkedTableSet = new HashSet<>();
originalLinkedTableSet.addAll(linkedTableSet);

// Build the list adapter for selecting linked tables
TableLinkAdapter linkAdapter = new TableLinkAdapter(getActivity(),
R.layout.table_link_row, tables, linkedTableSet);
View tableLinkView = inflater.inflate(R.layout.table_link, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
dialog.setView(tableLinkView);
TextView titleTextView = (TextView) tableLinkView.findViewById(R.id.tableLinkTitleTextView);
switch (table.getType()) {
case FEATURE:
titleTextView.setText(getString(R.string.geopackage_table_link_tiles_list_title));
break;
case TILE:
titleTextView.setText(getString(R.string.geopackage_table_link_features_list_title));
break;
}
ListView listView = (ListView) tableLinkView.findViewById(R.id.tableLinkListView);
listView.setAdapter(linkAdapter);

dialog.setPositiveButton(getString(R.string.button_ok_label),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int id) {

// Determine which links exist that were unchecked
Set<String> removedLinks = new HashSet<>();
removedLinks.addAll(originalLinkedTableSet);
removedLinks.removeAll(linkedTableSet);

// Determine which links were newly checked
Set<String> newLinks = new HashSet<>();
newLinks.addAll(linkedTableSet);
newLinks.removeAll(originalLinkedTableSet);

// Check if we need to unlink or linke tables
if(!removedLinks.isEmpty() || !newLinks.isEmpty()){

// Create a linker
GeoPackageManager manager = GeoPackageFactory.getManager(getActivity());
GeoPackage geoPackage = manager.open(table.getDatabase());
FeatureTileTableLinker linker = new FeatureTileTableLinker(geoPackage);

// Delete links
for(String removedLink: removedLinks){
switch(table.getType()) {
case FEATURE:
linker.deleteLink(table.getName(), removedLink);
break;
case TILE:
linker.deleteLink(removedLink, table.getName());
break;
}
}

// Create links
for(String newLink: newLinks){
switch(table.getType()) {
case FEATURE:
linker.link(table.getName(), newLink);
break;
case TILE:
linker.link(newLink, table.getName());
break;
}
}

// Close the GeoPackage and mark as changes made
geoPackage.close();
active.setModified(true);
}

}
}).setNegativeButton(getString(R.string.button_cancel_label),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog.show();

}

/**
* Add feature overlay table option
Expand Down Expand Up @@ -3381,9 +3529,9 @@ public void onClick(DialogInterface dialog, int which) {
* @param granted
*/
public void importGeoPackageExternalLinkAfterPermissionGranted(boolean granted) {
if(granted) {
if (granted) {
importGeoPackageExternalLink(importExternalName, importExternalUri, importExternalPath);
}else{
} else {
showDisabledExternalImportPermissionsDialog();
}
}
Expand Down Expand Up @@ -3914,4 +4062,64 @@ public boolean isChildSelectable(int i, int j) {

}

/**
* Table Link Adapter for displaying linkable tables
*/
private class TableLinkAdapter extends ArrayAdapter<String> {

/**
* Set of currently linked tables
*/
private final Set<String> linkedTables;

/**
* Constructor
* @param context
* @param textViewResourceId
* @param tables tables that can be linked
* @param linkedTables set of currently linked tables
*/
public TableLinkAdapter(Context context, int resource,
List<String> tables,
Set<String> linkedTables) {
super(context, resource, tables);
this.linkedTables = linkedTables;
}

@Override
public View getView(int position, View view, ViewGroup parent) {

if (view == null) {
view = inflater.inflate(R.layout.table_link_row, null);
}

final String linkTable = getItem(position);

CheckBox checkBox = (CheckBox) view
.findViewById(R.id.table_link_row_checkbox);
TextView tableName = (TextView) view
.findViewById(R.id.table_link_row_name);

// Add or remove the table from being clicked on checkbox changes
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
linkedTables.add(linkTable);
}else{
linkedTables.remove(linkTable);
}
}
});

// Set the initial values
checkBox.setChecked(linkedTables.contains(linkTable));
tableName.setText(linkTable);

return view;
}

}

}
20 changes: 20 additions & 0 deletions mapcache/src/main/res/layout/table_link.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tableLinkTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textSize="22sp" />

<ListView
android:id="@+id/tableLinkListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
20 changes: 20 additions & 0 deletions mapcache/src/main/res/layout/table_link_row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99D6D6"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/table_link_row_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/table_link_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="22sp" />

</LinearLayout>
3 changes: 3 additions & 0 deletions mapcache/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
<string name="geopackage_table_tiles_load_label">Load Tiles</string>
<string name="geopackage_table_create_feature_tiles_label">Create Tiles</string>
<string name="geopackage_table_add_feature_overlay_label">Add Tile Overlay</string>
<string name="geopackage_table_link_label">Linked Tables</string>
<string name="geopackage_table_link_features_list_title">Linked Feature Tables</string>
<string name="geopackage_table_link_tiles_list_title">Linked Tile Tables</string>
<string name="geopackage_table_edit_feature_overlay_label">Edit Tile Overlay</string>
<string name="geopackage_table_index_features_label">Feature Index</string>
<string name="geopackage_table_index_features_index_title">Index</string>
Expand Down

0 comments on commit cea0de2

Please sign in to comment.