Skip to content

Commit

Permalink
KML: cut/copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 20, 2014
1 parent 46167ed commit 348906c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
11 changes: 7 additions & 4 deletions OSMBonusPackDemo/res/menu/kml_item_menu.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_delete"
android:title="Delete"/>
android:id="@+id/menu_cut"
android:title="Cut"/>
<item
android:id="@+id/menu_duplicate"
android:title="Duplicate"/>
android:id="@+id/menu_copy"
android:title="Copy"/>
<item
android:id="@+id/menu_paste"
android:title="Paste"/>
<item
android:id="@+id/menu_behind"
android:title="Push behind"/>
Expand Down
56 changes: 33 additions & 23 deletions OSMBonusPackDemo/src/com/osmbonuspackdemo/KmlTreeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class KmlTreeActivity extends Activity {

KmlListAdapter listAdapter;
ListView listView;
KmlFeature kmlObject;
KmlFeature currentKmlFeature; //feature currently edited.
KmlFeature kmlClipboard; //link to the global KML clipboard.
protected static final int KML_TREE_REQUEST = 3;
int mItemPosition; //last item opened
EditText eHeader, eDescription, eOutlineColor, eFillColor;
Expand All @@ -37,20 +38,21 @@ public class KmlTreeActivity extends Activity {
listView = (ListView) findViewById(R.id.listviewKml);
registerForContextMenu(listView);

kmlObject = MapActivity.mKmlStack.peek();
currentKmlFeature = MapActivity.mKmlStack.peek();
kmlClipboard = MapActivity.mKmlClipboard;

eHeader = (EditText)findViewById(R.id.name);
eHeader.setText(kmlObject.mName);
eHeader.setText(currentKmlFeature.mName);

eDescription = (EditText)findViewById(R.id.description);
eDescription.setText(kmlObject.mDescription);
eDescription.setText(currentKmlFeature.mDescription);

Style style = null;
if (kmlObject.mStyle != null)
style = MapActivity.mKmlDocument.getStyle(kmlObject.mStyle);
if (currentKmlFeature.mStyle != null)
style = MapActivity.mKmlDocument.getStyle(currentKmlFeature.mStyle);

eOutlineColor = (EditText)findViewById(R.id.outlineColor);
if ((kmlObject.mObjectType == KmlFeature.LINE_STRING || kmlObject.mObjectType == KmlFeature.POLYGON) && style!=null){
if ((currentKmlFeature.mObjectType == KmlFeature.LINE_STRING || currentKmlFeature.mObjectType == KmlFeature.POLYGON) && style!=null){
mOutlineColorStyle = style.outlineColorStyle;
eOutlineColor.setText(mOutlineColorStyle.colorAsAndroidString());
} else {
Expand All @@ -59,15 +61,15 @@ public class KmlTreeActivity extends Activity {
}

eFillColor = (EditText)findViewById(R.id.fillColor);
if (kmlObject.mObjectType == KmlFeature.POLYGON && style!=null){
if (currentKmlFeature.mObjectType == KmlFeature.POLYGON && style!=null){
mFillColorStyle = style.fillColorStyle;
eFillColor.setText(mFillColorStyle.colorAsAndroidString());
} else {
LinearLayout lFillColorLayout = (LinearLayout)findViewById(R.id.fillColorLayout);
lFillColorLayout.setVisibility(View.GONE);
}

listAdapter = new KmlListAdapter(this, kmlObject);
listAdapter = new KmlListAdapter(this, currentKmlFeature);

// setting list adapter
listView.setAdapter(listAdapter);
Expand All @@ -76,7 +78,7 @@ public class KmlTreeActivity extends Activity {
listView.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
mItemPosition = position;
KmlFeature item = kmlObject.mItems.get(position);
KmlFeature item = currentKmlFeature.mItems.get(position);
Intent myIntent = new Intent(view.getContext(), KmlTreeActivity.class);
//myIntent.putExtra("KML", item);
MapActivity.mKmlStack.push(item.clone());
Expand All @@ -87,8 +89,8 @@ public class KmlTreeActivity extends Activity {
Button btnOk = (Button) findViewById(R.id.btnOK);
btnOk.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
kmlObject.mName = eHeader.getText().toString();
kmlObject.mDescription = eDescription.getText().toString();
currentKmlFeature.mName = eHeader.getText().toString();
currentKmlFeature.mDescription = eDescription.getText().toString();
if (mOutlineColorStyle != null){
String sColor = eOutlineColor.getText().toString();
try {
Expand Down Expand Up @@ -116,7 +118,7 @@ public void onClick(View view) {
case KML_TREE_REQUEST:
KmlFeature result = MapActivity.mKmlStack.pop();
if (resultCode == RESULT_OK) {
kmlObject.mItems.set(mItemPosition, result);
currentKmlFeature.mItems.set(mItemPosition, result);
listAdapter.notifyDataSetChanged();
}
break;
Expand All @@ -134,26 +136,34 @@ public void onClick(View view) {
@Override public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menu_delete:
kmlObject.removeItem(info.position);
case R.id.menu_cut: //=move to the emptied clipboard
kmlClipboard.mItems.clear();
kmlClipboard.add(currentKmlFeature.mItems.get(info.position));
currentKmlFeature.removeItem(info.position);
listAdapter.notifyDataSetChanged();
return true;
case R.id.menu_duplicate:
KmlFeature duplicate = kmlObject.mItems.get(info.position).clone();
kmlObject.add(duplicate);
case R.id.menu_copy:
KmlFeature copy = currentKmlFeature.mItems.get(info.position).clone();
kmlClipboard.mItems.clear();
kmlClipboard.mItems.add(copy);
return true;
case R.id.menu_paste:
for (KmlFeature kmlItem:kmlClipboard.mItems){
currentKmlFeature.add(kmlItem.clone());
}
listAdapter.notifyDataSetChanged();
return true;
case R.id.menu_behind:
if (info.position > 0){
KmlFeature kmlItem = kmlObject.removeItem(info.position);
kmlObject.mItems.add(info.position-1, kmlItem);
KmlFeature kmlItem = currentKmlFeature.removeItem(info.position);
currentKmlFeature.mItems.add(info.position-1, kmlItem);
listAdapter.notifyDataSetChanged();
}
return true;
case R.id.menu_front:
if (info.position < kmlObject.mItems.size()-1){
KmlFeature kmlItem = kmlObject.removeItem(info.position);
kmlObject.mItems.add(info.position+1, kmlItem);
if (info.position < currentKmlFeature.mItems.size()-1){
KmlFeature kmlItem = currentKmlFeature.removeItem(info.position);
currentKmlFeature.mItems.add(info.position+1, kmlItem);
listAdapter.notifyDataSetChanged();
}
return true;
Expand Down
9 changes: 6 additions & 3 deletions OSMBonusPackDemo/src/com/osmbonuspackdemo/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public class MapActivity extends Activity implements MapEventsReceiver, Location
protected FolderOverlay kmlOverlay; //root container of overlays from KML reading
protected static final int KML_TREE_REQUEST = 3;
public static KmlDocument mKmlDocument = new KmlDocument(); //made static to pass between activities
public static Stack<KmlFeature> mKmlStack = new Stack<KmlFeature>(); //passed between activities
public static Stack<KmlFeature> mKmlStack = new Stack<KmlFeature>(); //passed between activities, top is the current KmlFeature to edit.
public static KmlFeature mKmlClipboard; //passed between activities. Folder for multiple items selection.

static String SHARED_PREFS_APPKEY = "OSMNavigator";
static String PREF_LOCATIONS_KEY = "PREF_LOCATIONS";
Expand Down Expand Up @@ -304,8 +305,10 @@ public void onClick(View v) {
if (savedInstanceState != null){
//STATIC - mKmlDocument = savedInstanceState.getParcelable("kml");
updateUIWithKml();
} else {
//first launch: check if intent has been passed with a kml URI to load (url or file)
} else { //first launch:
mKmlClipboard = new KmlFeature();
mKmlClipboard.createAsFolder();
//check if intent has been passed with a kml URI to load (url or file)
Intent onCreateIntent = getIntent();
if (onCreateIntent.getAction().equals(Intent.ACTION_VIEW)){
String uri = onCreateIntent.getDataString();
Expand Down

0 comments on commit 348906c

Please sign in to comment.